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

how to make text bold in textview in android programmatically
android textview bold part of text
android textview underline
android:textstyle
android textview bold xml
android string bold programmatically
how to set font style for textview in android programmatically
textview android

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?


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?

How to set the font style to bold, italic and underlined in an Android , Way 1 – make Android TextView bold using android:textStyle attribute. 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. How to set TextView style (bold ot italic) with in Java and without using XML layout?. In other words I need to write android:textStyle with Java.


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

4 Ways to make Android TextView Bold, 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? How to set the font style to bold, italic and underlined in an Android TextView? (6) 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?


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

Working with the TextView, You can specify any one of them as the value for the android:typeface attribute in the XML: The possible values are: normal , bold , italic . android:textStyle=" bold" />. A sampling of styles can be seen below: style We can change the text color, change the typeface, add an underline, etc, and apply these to only certain � Questions: How to set TextView style (bold ot italic) with in Java and without using XML layout? In other words I need to write android:textStyle with Java. Answers: 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


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>

How to Change the Text Style of a Substring, How to Change the Text Style of a Substring - Android Studio Tutorial parts of a TextView Duration: 6:11 Posted: Jun 8, 2018 Set Button Text style to Bold Italic in android programmatically.Apply different types of font style on button widget in android.Changing buttonClick event.


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

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

android.text.SpannableString.setSpan java code examples, 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� How to set the font style to bold, italic and underlined in an Android TextView? 148. Set underline text to TextView in android programmatically.


android.widget.TextView.setPaintFlags java code examples, How to set the font style to bold, italic and underlined in an Android TextView? String tempString="Copyright"; TextView text=(TextView)findViewById(R.id.text);� Starting from Android-Studio 3.0 its very easy to change font family. Using support library 26, it will work on devices running Android API version 16 and higher . Create a folder font under res directory .Download the font which ever you want and paste it inside font folder.


SpannableString Text Style, How to set the font style to bold, italic and underlined in an Android TextView? TextView tv = (TextView)findViewById(R.id.textViewOne); tv.setTypeface(null� To retain current font in addition to text style: textView.apply { setTypeface(typeface, Typeface.NORMAL) // or setTypeface(typeface, Typeface.BOLD) // or setTypeface(typeface, Typeface.ITALIC) // or setTypeface(typeface, Typeface.BOLD_ITALIC) }


In this video we will learn, how to change the text style of a substring, by using the Duration: 6:11 Posted: Jun 8, 2018 The android:textStyle is used to set the style among bold, italic, and normal. If you want to set bold and italic, use android:textStyle = “bold|italic”. The android:textAppearance attribute is used to set the style on a TextView, which includes its own