Is there a way to set the textStyle
attribute of a TextView
programmatically? There doesn\'t appear to be a setTextStyle()
method.
This worked for me
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
or
textview.setTypeface(Typeface.DEFAULT_BOLD);
So many way to achieve this task some are below:-
1.
String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text_view_str));
2.
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
3.
SpannableString spannablecontent=new SpannableString(o.content.toString());
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
0,spannablecontent.length(), 0);
// set Text here
tt.setText(spannablecontent);
4.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="boldText">
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="normalText">
<item name="android:textStyle">normal</item>
<item name="android:textColor">#C0C0C0</item>
</style>
</resources>
tv.setTextAppearance(getApplicationContext(), R.style.boldText);
or if u want through xml
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
textview.setTypeface(Typeface.DEFAULT_BOLD);
setTypeface is the Attribute textStyle.
As Shankar V added, to preserve the previously set typeface attributes you can use:
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);
As mentioned here, this feature is not currently supported.
Search for setTextAppearance
or also setTextTypeface
. There is similar question on stackoverflow: How to change a TextView's style at runtime