nithin
nithin

Reputation: 5171

How do you change text to bold in Android?

How do you change text/font settings in an Android TextView?

For example, how do you make the text bold?

Upvotes: 517

Views: 606400

Answers (22)

EpicPandaForce
EpicPandaForce

Reputation: 81529

textView.setPaintFlags(textView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG)

To remove, use

textView.setPaintFlags(textView.getPaintFlags() & ~Paint.FAKE_BOLD_TEXT_FLAG)

Or in Kotlin:

fun TextView.makeBold() {
    this.paintFlags = this.paintFlags or Paint.FAKE_BOLD_TEXT_FLAG
}

fun TextView.removeBold() {
    this.paintFlags = this.paintFlags and (Paint.FAKE_BOLD_TEXT_FLAG.inv())
}

Upvotes: -2

user15052429
user15052429

Reputation:

Through XML:

 android:textStyle="bold"

Through Java:

//Let's say you have a textview 
textview.setTypeface(null, Typeface.BOLD);

Upvotes: 21

Arty Morris
Arty Morris

Reputation: 99

You can do this

ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);

Upvotes: 0

Salman Nazir
Salman Nazir

Reputation: 2837

In Kotlin we can do in one line

 TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

Upvotes: 2

Udara Abeythilake
Udara Abeythilake

Reputation: 1213

From the XML you can set the textStyle to bold as below

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Bold text"
   android:textStyle="bold"/>

You can set the TextView to bold programmatically as below

textview.setTypeface(Typeface.DEFAULT_BOLD);

Upvotes: 28

Hoang Nguyen Viet
Hoang Nguyen Viet

Reputation: 111

in file .xml, set

android:textStyle="bold" 

will set text type is bold.

Upvotes: 10

Athira Reddy
Athira Reddy

Reputation: 1054

4 ways to make Android TextView bold- Full answer is here.

  1. Using android:textStyle attribute

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> Use bold|italic for bold and italic.

  2. using setTypeface() method

    textview2.setTypeface(null, Typeface.BOLD);
    textview2.setText("TEXTVIEW 2");
    
  3. HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24.

     String html="This is <b>TEXTVIEW 3</b>";
     textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
    

Upvotes: 8

Rajesh Naddy
Rajesh Naddy

Reputation: 1105

In my case, Passing value through string.xml worked out with html Tag..

<string name="your_string_tag"> <b> your_text </b></string>

Upvotes: 6

Sam p
Sam p

Reputation: 31

editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);

will set both typface as well as style to Bold.

Upvotes: 3

Phobos
Phobos

Reputation: 9557

To do this in the layout.xml file:

android:textStyle

Examples:

android:textStyle="bold|italic"

Programmatically the method is:

setTypeface(Typeface tf)

Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTypeface(Typeface, int) to get the appearance that you actually want.

Upvotes: 653

saeed
saeed

Reputation: 1955

Simply you can do the following:

Set the attribute in XML

  android:textStyle="bold"

Programatically the method is:

TextView Tv = (TextView) findViewById(R.id.TextView);

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);

Tv.setTypeface(boldTypeface);

Hope this helps you thank you.

Upvotes: 89

Dan Tilakaratne
Dan Tilakaratne

Reputation: 71

Assuming you are a new starter on Android Studio, Simply you can get it done in design view XML by using

android:textStyle="bold"          //to make text bold
android:textStyle="italic"        //to make text italic
android:textStyle="bold|italic"   //to make text bold & italic

Upvotes: 6

sabbibJAVA
sabbibJAVA

Reputation: 1076

The best way to go is:

TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);

Upvotes: 9

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

In the ideal world you would set the text style attribute in you layout XML definition like that:

<TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"/>

There is a simple way to achieve the same result dynamically in your code by using setTypeface method. You need to pass and object of Typeface class, which will describe the font style for that TextView. So to achieve the same result as with the XML definition above you can do the following:

TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);

The first line will create the object form predefined style (in this case Typeface.BOLD, but there are many more predefined). Once we have an instance of typeface we can set it on the TextView. And that's it our content will be displayed on the style we defined.

I hope it helps you a lot.For better info you can visit

http://developer.android.com/reference/android/graphics/Typeface.html

Upvotes: 16

Amaresh Jana
Amaresh Jana

Reputation: 742

You can use this for font

create a Class Name TypefaceTextView and extend the TextView

private static Map mTypefaces;

public TypefaceTextView(final Context context) {
    this(context, null);
}

public TypefaceTextView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    if (mTypefaces == null) {
        mTypefaces = new HashMap<String, Typeface>();
    }

    if (this.isInEditMode()) {
        return;
    }

    final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
    if (array != null) {
        final String typefaceAssetPath = array.getString(
                R.styleable.TypefaceTextView_customTypeface);

        if (typefaceAssetPath != null) {
            Typeface typeface = null;

            if (mTypefaces.containsKey(typefaceAssetPath)) {
                typeface = mTypefaces.get(typefaceAssetPath);
            } else {
                AssetManager assets = context.getAssets();
                typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                mTypefaces.put(typefaceAssetPath, typeface);
            }

            setTypeface(typeface);
        }
        array.recycle();
    }
}

paste the font in the fonts folder created in the asset folder

<packagename.TypefaceTextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        android:gravity="center"
        android:text="TRENDING TURFS"
        android:textColor="#000"
        android:textSize="20sp"
        app:customTypeface="fonts/pompiere.ttf" />**here pompiere.ttf is the font name**

Place the lines in the parent layout in the xml

 xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
xmlns:custom="http://schemas.android.com/apk/res-auto"

Upvotes: 5

Sudipta Som
Sudipta Som

Reputation: 6567

Here is the solution

TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);

Upvotes: 382

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

In XML

android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic

You can only use specific fonts sans, serif & monospace via xml, Java code can use custom fonts

android:typeface="monospace" // or sans or serif

Programmatically (Java code)

TextView textView = (TextView) findViewById(R.id.TextView1);

textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
                                         //font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
                                         //font style & text style(bold & italic)

Upvotes: 54

Seven
Seven

Reputation: 375

Define a new style with the format you want in the style.xml file in the values folder

<style name="TextViewStyle" parent="AppBaseTheme">
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#5EADED</item>

</style>

Then apply this style to the TextView by writing the following code with the properties of the TextView

style="@style/TextViewStyle"

Upvotes: 11

Niko
Niko

Reputation: 8153

For case where you are using custom fonts, but do not have bold typeface for the font you can use:

myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");

Upvotes: 28

HatemTmi
HatemTmi

Reputation: 1078

It's very easy

setTypeface(Typeface.DEFAULT_BOLD);

Upvotes: 22

noelicus
noelicus

Reputation: 15055

If you're drawing it then this will do it:

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);

Upvotes: 17

koljaTM
koljaTM

Reputation: 10262

Set the attribute

android:textStyle="bold"

Upvotes: 22

Related Questions