How to set TextView textStyle such as bold, italic

AndroidTextviewStyles

Android Problem Overview


How to set TextView style (bold or italic) within Java and without using the XML layout?

In other words, I need to write android:textStyle with Java.

Android Solutions


Solution 1 - Android

textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);

To keep the previous typeface

textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)

Solution 2 - Android

Try this to set on TextView for bold or italic

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

Solution 3 - Android

###Programmatically: You can do programmatically using setTypeface():

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

###XML: You can set Directly in XML file in <TextView /> like:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
            

Solution 4 - Android

You have two options:

Option 1 (only works for bold, italic and underline):

String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(Html.fromHtml(s));

Option 2:

Use a Spannable; it is more complicated, but you can dynamically modify the text attributes (not only bold/italic, also colors).

Solution 5 - Android

###Programmatically: You can do programmatically using setTypeface() method:

Below is the code for default Typeface

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

and if you want to set custom Typeface:

textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic

###XML: You can set directly in XML file in <TextView /> like this:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

Or you can set your fav font (from assets). for more info see link

Solution 6 - Android

TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);

now set the textview properties..

text.setTypeface(null, Typeface.BOLD);  //-- for only bold the text
text.setTypeface(null, Typeface.BOLD_ITALIC);  //-- for  bold & italic the text
text.setTypeface(null, Typeface.ITALIC);  // -- for  italic the text

Solution 7 - Android

Simply if you want to make text bold. write this line in your layout in text view property

android:textStyle="bold"

Solution 8 - Android

It would be

yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);

and italic should be able to be with replacing Typeface.DEFAULT_BOLD with Typeface.DEFAULT_ITALC.

Let me know how it works.

Solution 9 - Android

try this to set your TextView style by java code

txt1.setTypeface(null,Typeface.BOLD_ITALIC);

Solution 10 - Android

TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);

Solution 11 - Android

Try this:

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

Solution 12 - Android

You can set the different typeface using the example given below -

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

Or if you want to set a different font and its typeface . Add it to asset or raw folder and then use it like

  Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
  tv1.setTypeface(face);

  Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
  tv2.setTypeface(face1);

Solution 13 - Android

Use textView.setTypeface(Typeface tf, int style); to set style property of the TextView. See the developer documentation for more info.

Solution 14 - Android

Try this:

TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);

Solution 15 - Android

Standard way to do this is to use the custom styles. Ex-

In styles.xml add the following.

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyApp.TextAppearance.LoginText">
    <item name="android:textStyle">bold|italic</item>
</style>

Apply this style to your TextView as follows.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/MyApp.TextAppearance.LoginText" />

Solution 16 - Android

And as explained here Android Developers String Resources if you need to use parameters in your styled text resource, you have to escape the opening brackets

<resources>
<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
</resources>

and call formatHtml(string)

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);

Solution 17 - Android

One way you can do is :

myTextView.setTypeface(null, Typeface.ITALIC);
myTextView.setTypeface(null, Typeface.BOLD_ITALIC);
myTextView.setTypeface(null, Typeface.BOLD);
myTextView.setTypeface(null, Typeface.NORMAL);
    

Another option if you want to keep the previous typeface and don't want to lose previously applied then:

myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD);        
myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); 

Solution 18 - Android

textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);

To keep the previous typeface

textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)

Solution 19 - Android

The easiest way you can do based on the style selection criteria is:

String pre = "", post = "";

if(isBold){
    pre += "<b>"; post += "</b>";
}
if(isItalic){
    pre += "<i>"; post += "</i>";
}
if(isUnderline){
    pre += "<u>"; post += "</u>";
}

textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
// you can also use it with EidtText
editText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));

Solution 20 - Android

You can try like this:

<string name="title"><u><b><i>Your Text</i></b></u></string>

Solution 21 - Android

While using simplified tags with AndroidX consider using HtmlCompat.fromHtml()

String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"    
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(HtmlCompat.fromHtml(s, FROM_HTML_MODE_LEGACY));

Solution 22 - Android

Since I want to use a custom font only conjunction of several answers works for me. Obviously settings in my layout.xml like android:textStlyle="italic" was ignored by AOS. So finally I had to do as follows: in strings.xml the target string was declared as:

<string name="txt_sign"><i>The information blah blah ...</i></string>

then additionally in code:

TextView textSign = (TextView) findViewById(R.id.txt_sign);
FontHelper.setSomeCustomFont(textSign);
textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);

I didn't try the Spannable option (which I assume MUST work) but

textSign.setText(Html.fromHtml(getString(R.string.txt_sign))) 

had no effect. Also if I remove the italic tag from strings.xml leaving the setTypeface() all alone it has no effect either. Tricky Android...

Solution 23 - Android

In my case:

1 - set text

2 - set typeface

holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);

Solution 24 - Android

This is the only thing that worked for me on a OnePlus 5T configured with the OnePlus Slate™ font:

textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));

Other methods would make it fall back to Roboto when either BOLD or NORMAL.

Solution 25 - Android

Best way is to define it in styles.xml

<style name="common_txt_style_heading" parent="android:style/Widget.TextView">
        <item name="android:textSize">@dimen/common_txtsize_heading</item>
        <item name="android:textColor">@color/color_black</item>
        <item name="android:textStyle">bold|italic</item>
</style>

And update it in TextView

  <TextView
     android:id="@+id/txt_userprofile"
     style="@style/common_txt_style_heading"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="@dimen/margin_small"
     android:text="@string/some_heading" />

Solution 26 - Android

AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);

Use above method to set the typeface programmatically.

Solution 27 - Android

  1. You can set it with TypeFace.
  2. You can directly use in strings.xml(in your values folder)
  3. You can String myNewString = " This is my bold text This is my italics string This is my underlined string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustMeView Question on Stackoverflow
Solution 1 - AndroidTanmay MandalView Answer on Stackoverflow
Solution 2 - AndroidNiranj PatelView Answer on Stackoverflow
Solution 3 - AndroidPratik ButaniView Answer on Stackoverflow
Solution 4 - AndroidGabriel NegutView Answer on Stackoverflow
Solution 5 - AndroidakshayView Answer on Stackoverflow
Solution 6 - AndroidAkash ThakkarView Answer on Stackoverflow
Solution 7 - Androiduser6430425View Answer on Stackoverflow
Solution 8 - AndroidDustinRileyView Answer on Stackoverflow
Solution 9 - AndroidNikhilView Answer on Stackoverflow
Solution 10 - AndroidKhushiView Answer on Stackoverflow
Solution 11 - Androidmohamad sheikhiView Answer on Stackoverflow
Solution 12 - AndroidRaviView Answer on Stackoverflow
Solution 13 - AndroidDinesh SharmaView Answer on Stackoverflow
Solution 14 - AndroidBlack_shadowView Answer on Stackoverflow
Solution 15 - AndroidShivanand DarurView Answer on Stackoverflow
Solution 16 - AndroidAngelo NodariView Answer on Stackoverflow
Solution 17 - AndroidparamView Answer on Stackoverflow
Solution 18 - AndroidAmirhfView Answer on Stackoverflow
Solution 19 - AndroidmuzamilView Answer on Stackoverflow
Solution 20 - AndroidVishal VaishnavView Answer on Stackoverflow
Solution 21 - AndroidaleksandrbelView Answer on Stackoverflow
Solution 22 - AndroidStanView Answer on Stackoverflow
Solution 23 - AndroidFlipNovidView Answer on Stackoverflow
Solution 24 - Androiddroid256View Answer on Stackoverflow
Solution 25 - AndroidFaheemView Answer on Stackoverflow
Solution 26 - AndroidTulsiView Answer on Stackoverflow
Solution 27 - AndroidUjjwal BassiView Answer on Stackoverflow