How to set TextView textStyle such as bold, italic
Asked Answered
C

30

959

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.

Catercornered answered 1/6, 2011 at 11:41 Comment(0)
T
2101
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)
Trouvaille answered 1/6, 2011 at 12:4 Comment(9)
To remove the style can be used Typeface.NORMALTroglodyte
If you do that, you'll lose any previous typeface. To keep the previous one, do something like textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);Encounter
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL); will not remove bold or italic styling from a TextView. You will need to use textView.setTypeface(null, Typeface.NORMAL); for that.Averroism
To keep any previous typeface but to get rid of bold or italic style, use following code : textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);Freewheeling
Thanks @Shnkc, You pointed me to the right direction. Actually you just need: textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL));Inspector
You mean, you can't just do a bitwise OR when you want both bold and italic?Lupine
@Encounter What do you mean by losing the typeface? If I set a custom font, will it go back to the default ones of the OS ?Gourmont
Is there a "semi bold"? Font weight of 500?Conversable
for kotlin: textView.typeface = Typeface.create(textView.typeface, if (isSelected) Typeface.BOLD else Typeface.NORMAL)Adjournment
C
290

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);
Centripetal answered 1/6, 2011 at 11:48 Comment(3)
Incidentally, if you want to clear an existing typeface style, you'll need to do something different, e.g.: tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL));Tevere
@Tevere tv.setTypeface(null, Typeface.BOLD); won't this do the same(clear an existing typeface style)?Disbar
Passing null into setTypeface() means that the TextView uses a hard-coded default that may be different from the Typeface previously set.Tevere
B
166

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"
Bacteriostat answered 28/3, 2013 at 7:34 Comment(3)
Questioner asked how to do it without using XML layout.Bundesrat
Check Question with in Java and without using XML By the way It will help to others also.Bacteriostat
Yes. I came here via Google and it just helped me. Thanks :)Invisible
I
103

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).

Internal answered 1/6, 2011 at 11:47 Comment(9)
Why not use the typeFace attribute?Unvoice
With typeFace you can set a single style for the whole text.Internal
when I am trying in my custom row its not getting why? String s1 = "<b>You are at:</b>"; holder.address = (TextView) convertView.findViewById(R.id.address_text_view); holder.address.setText(Html.fromHtml(s1)+ track.getAddress());Deenadeenya
This method is great partial text styling. Like a quote inside a large textview.Grolier
The first method doesn't work in my case private void createTextView(String title, String text) { textView = new TextView(this); textView.setTextSize(17); textView.setText(Html.fromHtml("<b>" + title + "</b>") + " : " + text); }Kelm
Option 1 is not works when we append some other text with this bolded text so in that case refer Option 2Orvalorvan
The spannable and fromHTML options may slow down typing / setText if the text is big due to layout recalculations. Avoid it if you have other methods available.Rhynchocephalian
tv.setText(Html.fromHtml("Test 1 2 3")); This code can also be used easily.Supplant
<br> also works if you're looking for quick new lines between headingsUnvoice
T
54

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

Tessler answered 14/5, 2015 at 4:36 Comment(1)
I've been trying to use this way but when font is set to Typeface.BOLD, everything starts to act weird. Text gets collapsed (aligned vertically instead of horizontal, totally unreadable) and buttons stop responding, messes with listeners.. it goes to normal when i let the custom font Typeface.NORMAL.Cedar
F
15
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
Fetiparous answered 1/6, 2011 at 13:0 Comment(0)
P
15

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);
Pickmeup answered 12/3, 2020 at 10:54 Comment(0)
G
13

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

android:textStyle="bold"
Grosswardein answered 7/6, 2016 at 9:5 Comment(0)
S
11

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.

Salsify answered 1/6, 2011 at 11:47 Comment(0)
R
11

Try this:

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Rules answered 16/2, 2019 at 5:14 Comment(0)
K
10

try this to set your TextView style by java code

txt1.setTypeface(null,Typeface.BOLD_ITALIC);
Kurdistan answered 1/6, 2011 at 11:49 Comment(0)
E
10
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
Extinct answered 13/7, 2012 at 6:7 Comment(0)
V
8
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)
Velate answered 9/2, 2019 at 14:23 Comment(0)
T
7

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

Todd answered 1/6, 2011 at 11:48 Comment(0)
W
6

Try this:

TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
Weiner answered 14/10, 2014 at 20:59 Comment(0)
B
4

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" />
Billboard answered 15/4, 2014 at 14:14 Comment(0)
O
4

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);
Overblouse answered 14/7, 2015 at 15:50 Comment(0)
A
4

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); 
Ananna answered 27/2, 2017 at 11:22 Comment(0)
O
4

You can try like this:

<string name="title"><u><b><i>Your Text</i></b></u></string>
Omnirange answered 31/7, 2017 at 7:42 Comment(0)
P
4

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));
Previous answered 15/3, 2019 at 2:53 Comment(0)
B
3

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));
Blas answered 12/3, 2020 at 10:28 Comment(0)
S
2

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...

Sudoriferous answered 25/8, 2014 at 9:51 Comment(0)
P
2

In my case:

1 - set text

2 - set typeface

holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
Panta answered 28/9, 2017 at 17:15 Comment(0)
A
2
//leveraging the extension functions
fun TextView.makeBold(){
    this.setTypeface(this.typeface,Typeface.BOLD)
}


yourTextView.makeBold()
Aegisthus answered 14/11, 2022 at 10:20 Comment(0)
C
1

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.

Contemporary answered 29/12, 2017 at 20:57 Comment(0)
R
0

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" />
Repurchase answered 19/8, 2015 at 8:37 Comment(0)
M
0
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);

Use above method to set the typeface programmatically.

Marymarya answered 2/1, 2017 at 8:48 Comment(0)
M
0

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

Madera answered 13/12, 2019 at 6:23 Comment(0)
C
0

If you have another font variation settings and don't want to revert back to Roboto use this code:

val span = SpannableString(textView.text.toString())
span.setSpan(StyleSpan(Typeface.BOLD), 0, span.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
textView.text = span
Chaetognath answered 14/11, 2023 at 5:49 Comment(0)
C
0

As I have been looking for a solution myself and I didn't see what I came up with over here, I'll share:

You can use String resources

<resources>
    <string name="text">This text is <i>italic</i>. For bold use <b>this</b>. And you can underline with <u>this tag</u>.</string>
</resources>

Then you can directly link in your layout XML to this resource:

<TextView
        android:id="@+id/some_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="12dp" />
Cumshaw answered 5/2 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.