TextView-এ HTML ট্যাগ ব্যবহার করে মাল্টিপল স্টাইলিং
অ্যান্ড্রয়েডের Html.fromHtml() মেথড ব্যবহার করে একটি TextView-এ বোল্ড, ইট্যালিক, রঙিন টেক্সট, ছোট ফন্ট ইত্যাদি একসাথে প্রয়োগ করা যায়। এছাড়া স্ট্রিং রিসোর্সে CDATA ব্যবহার করেও এটি করা সম্ভব।
ধাপ ১: প্রজেক্ট তৈরি করুন
সাধারণভাবে অ্যান্ড্রয়েড স্টুডিওতে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউটে TextView যোগ করুন
res/layout/activity_main.xml ফাইলে তিনটি TextView যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="@id/textView2"
android:layout_marginBottom="16dp"
android:padding="4dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="4dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginTop="16dp"
android:padding="4dp"
android:textSize="16sp"/>
</RelativeLayout>
ধাপ ৩: জাভা কোডে HTML স্টাইলিং
src/MainActivity.java ফাইলে:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView1, textView2, textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
// পদ্ধতি ১: সরাসরি HTML স্ট্রিং
textView1.setText(Html.fromHtml("Multiple style: " +
"<b>bold text</b>, <i>italic text</i>, " +
"<small>small text</small>, " +
"<font color='blue'>Text Color</font>, " +
"<font color='green'><b>Bold with color</b></font>"));
// পদ্ধতি ২: Spanned অবজেক্ট ব্যবহার
Spanned text = Html.fromHtml("Multiple style: " +
"<b>bold text</b>, <i>italic text</i>, " +
"<small>small text</small>, " +
"<font color='blue'>Text Color</font>, " +
"<font color='green'><b>Bold with color</b></font>");
textView2.setText(text);
// পদ্ধতি ৩: strings.xml থেকে
textView3 = findViewById(R.id.textView3);
textView3.setText(Html.fromHtml(getString(R.string.textStyle)));
}
}
ধাপ ৪: স্ট্রিং রিসোর্সে CDATA ব্যবহার
res/values/strings.xml ফাইলে:
<resources>
<string name="app_name">Sample</string>
<string name="textStyle"><![CDATA[
Multiple style: <b>bold text</b>, <i>italic text</i>,
<small>small text</small>, <font color="blue">Text Color</font>,
<font color="green"><b>Bold with color</b></font>
]]></string>
</resources>
মূল পয়েন্ট
Html.fromHtml()API 24+ এ deprecated, নতুন সংস্করণেHtml.fromHtml(String, int)ব্যবহার করুন- সমর্থিত ট্যাগ: <b>, <i>, <u>, <font>, <small>, <big>, <a> ইত্যাদি
- CDATA সেকশন XML পার্সিং এরর এড়াতে সাহায্য করে