Android TextView में मल्टीपल टेक्स्ट स्टाइल्स का उपयोग
एक ही TextView में बोल्ड, इटैलिक, छोटा फ़ॉन्ट, टेक्स्ट कलर आदि अलग-अलग स्टाइल्स लागू करने के तीन तरीके सीखें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट में तीन TextView जोड़ें
res/layout/activity_main.xml:
<?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>
चरण 3: 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);
textView3 = findViewById(R.id.textView3);
// तरीका 1: Html.fromHtml() सीधे जावा कोड में
textView1.setText(Html.fromHtml(
"Multiple styles: " +
"<b>Bold text</b>, " +
"<i>Italic text</i>, " +
"<small>Small text</small>, " +
"<font color='blue'>Blue text</font>, " +
"<font color='green'><b>Green bold</b></font>"
));
// तरीका 2: Spanned ऑब्जेक्ट बनाकर
Spanned text = Html.fromHtml(
"Multiple styles: " +
"<b>Bold text</b>, " +
"<i>Italic text</i>, " +
"<small>Small text</small>, " +
"<font color='blue'>Blue text</font>, " +
"<font color='green'><b>Green bold</b></font>"
);
textView2.setText(text);
// तरीका 3: strings.xml से HTML स्ट्रिंग लोड करना
textView3.setText(Html.fromHtml(getString(R.string.textStyle)));
}
}
चरण 4: strings.xml में HTML स्ट्रिंग परिभाषित करें
res/values/strings.xml:
<resources>
<string name="app_name">Sample</string>
<string name="textStyle"><![CDATA[
Multiple styles: <b>Bold text</b>, <i>Italic text</i>,
<small>Small text</small>, <font color="blue">Blue text</font>,
<font color="green"><b>Green bold</b></font>
]]></string>
</resources>
नोट: Android 7.0 (API 24) सेHtml.fromHtml()के लिएHtml.FROM_HTML_MODE_LEGACYफ्लैग का उपयोग करें।
ऐप चलाएं
डिवाइस पर चलाएं। तीनों TextView में मिश्रित स्टाइल्स दिखाई देंगी।