How do you change text/font settings in an Android TextView
?
For example, how do you make the text bold
In XML
android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic
You can only use specific fonts sans
, serif
& monospace
via xml, Java code can use custom fonts
android:typeface="monospace" // or sans or serif
Programmatically (Java code)
TextView textView = (TextView) findViewById(R.id.TextView1);
textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
//font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
//font style & text style(bold & italic)
Define a new style with the format you want in the style.xml file in the values folder
<style name="TextViewStyle" parent="AppBaseTheme">
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#5EADED</item>
</style>
Then apply this style to the TextView by writing the following code with the properties of the TextView
style="@style/TextViewStyle"
The best way to go is:
TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);
Set the attribute
android:textStyle="bold"
If you're drawing it then this will do it:
TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
4 ways to make Android TextView bold- Full answer is here.
Using android:textStyle attribute
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTVIEW 1"
android:textStyle="bold"
/>
Use bold|italic for bold and italic.
using setTypeface() method
textview2.setTypeface(null, Typeface.BOLD);
textview2.setText("TEXTVIEW 2");
HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24.
String html="This is <b>TEXTVIEW 3</b>";
textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));