530

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?

5
  • does it work to set just one of them?
    – falstro
    Jan 7, 2011 at 7:47
  • yes working fine i also want to make it under line.
    – d-man
    Jan 7, 2011 at 7:48
  • 6
    textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    – bCliks
    Sep 11, 2013 at 5:50
  • 21
    tv.setTypeface(null, Typeface.BOLD_ITALIC);
    – user2742371
    Oct 30, 2013 at 13:54

12 Answers 12

397

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

3
  • 3
    I checked it in 2.1. So at least it should work from 2.1 and above Oct 14, 2012 at 2:03
  • 1
    Can consider using new StyleSpan(Typeface.BOLD_ITALIC) Jul 2, 2016 at 16:11
  • Why it's not work on dynamic concatenated string? It's wired that some numbers appeared....
    – AuBee
    Oct 14, 2016 at 22:54
308

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?

3
  • 52
    for under line.. textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    – bCliks
    Sep 11, 2013 at 5:51
  • 2
    @bala be aware that your solution always underlines the whole text, so it's not feasible in cases when one wants to underline just a portion of it. Apr 2, 2014 at 18:14
  • Using xml instead of doing it by code is preferred when possible. See Andro Selva's answer below Oct 23, 2020 at 16:05
179

Or just like this in Kotlin:

val tv = findViewById(R.id.textViewOne) as TextView
tv.setTypeface(null, Typeface.BOLD_ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD)
// OR
tv.setTypeface(null, Typeface.ITALIC)
// AND
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG

Or in Java:

TextView tv = (TextView)findViewById(R.id.textViewOne);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD);
// OR
tv.setTypeface(null, Typeface.ITALIC);
// AND
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

Keep it simple and in one line :)

2
  • 2
    By inserting the kotlinx.android.synthetic package for the view you are working with, the findViewByID is not necessary in Kotlin, making each of the setTypeface lines: textViewOne.setTypeface(...)
    – cren90
    Dec 11, 2017 at 22:50
  • is paintFlags necessary ? It's working without that
    – Prabs
    Feb 13, 2019 at 6:27
86

For bold and italic whatever you are doing is correct for underscore use following code

HelloAndroid.java

 package com.example.helloandroid;

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.SpannableString;
 import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textview = (TextView)findViewById(R.id.textview);
    SpannableString content = new SpannableString(getText(R.string.hello));
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    textview.setText(content);
}
}

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/hello"
android:textStyle="bold|italic"/>

string.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="hello">Hello World, HelloAndroid!</string>
  <string name="app_name">Hello, Android</string>
</resources>
1
  • To remove the underline pass Null value instead of the new UnderlineSpan() as following content.setSpan(null, 0, content.length(), 0); May 17, 2015 at 8:54
51

This is an easy way to add an underline, while maintaining other settings:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
1
  • Be aware that this solution always underlines the whole text, so it's not feasible in cases when one wants to underline just a portion of it. Apr 2, 2014 at 18:15
48

Programmatialy:

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 like:

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

If you are reading that text from a file or from the network.

You can achieve it by adding HTML tags to your text like mentioned

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>

and then you can use the HTML class that processes HTML strings into displayable styled text.

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
1
21

Without quotes works for me:

<item name="android:textStyle">bold|italic</item>
14

You can achieve it easily by using Kotlin's buildSpannedString{} under its core-ktx dependency.

val formattedString = buildSpannedString {
    append("Regular")
    bold { append("Bold") }
    italic { append("Italic") }
    underline { append("Underline") }
    bold { italic {append("Bold Italic")} }
}

textView.text = formattedString
0
13

Just one line of code in xml

        android:textStyle="italic"
5
    style="?android:attr/listSeparatorTextViewStyle
  • by making this style, u can achieve underlining
0

The community answer and the answer by Vivek have introduced the SpannableString class. This works, but what if you want more flexibility to build the string piece by piece, with different styles/effects in each piece, in StringBuilder style?

Is there something like StringBuilder for spannable strings? It turns out there is. Morgan Koh's answer explains it for Kotlin. In Java, we can do it too, something like

  SpannableStringBuilder myString = new SpannableStringBuilder().append("this in bold",boldSpan,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  myString.append("\n\nThis line in regular text\n\n");

Not the answer you're looking for? Browse other questions tagged or ask your own question.