Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the font style to bold, italic and underlined in an Android TextView?

I want to make a TextView's content bold, italic and underlined. I tried the following code and it works, but doesn't underline.

<Textview android:textStyle="bold|italic" .. 

How do I do it? Any quick ideas?

like image 972
d-man Avatar asked Jan 07 '11 07:01

d-man


People also ask

How do you bold and italicize text on Android?

android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .

How do I make text bold in TextView?

Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .

How do you underline text in TextView?

You can also underline a portion of the text via code, it can be done by creating a SpannableString and then setting it as the TextView text property: SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");text. setSpan(new UnderlineSpan(), 25, 6, 0);textView. setText(text);


2 Answers

This should make your TextView bold, underlined and italic at the same time.

strings.xml

<resources>     <string name="register"><u><b><i>Copyright</i></b></u></string> </resources> 

To set this String to your TextView, do this in your main.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/textview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:text="@string/register" /> 

or In JAVA,

TextView textView = new TextView(this); textView.setText(R.string.register); 

Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So in that case SpannableString comes into action.

String tempString="Copyright"; TextView text=(TextView)findViewById(R.id.text); SpannableString spanString = new SpannableString(tempString); spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0); spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0); spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0); text.setText(spanString); 

OUTPUT

enter image description here

like image 50
12 revs, 2 users 86% Avatar answered Oct 01 '22 06:10

12 revs, 2 users 86%


I don't know about underline, but for bold and italic there is "bolditalic". There is no mention of underline here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle

Mind you that to use the mentioned bolditalic you need to, and I quote from that page

Must be one or more (separated by '|') of the following constant values.

so you'd use bold|italic

You could check this question for underline: Can I underline text in an android layout?

like image 29
Nanne Avatar answered Oct 01 '22 07:10

Nanne