How do you change text to bold in Android?
AndroidTextTextviewAndroid Problem Overview
How do you change text/font settings in an Android TextView
?
For example, how do you make the text bold?
Android Solutions
Solution 1 - Android
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.
Solution 2 - Android
Here is the solution
TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);
Solution 3 - Android
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.
Solution 4 - Android
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)
Solution 5 - Android
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>");
Solution 6 - Android
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);
Solution 7 - Android
Set the attribute
android:textStyle="bold"
Solution 8 - Android
It's very easy
setTypeface(Typeface.DEFAULT_BOLD);
Solution 9 - Android
If you're drawing it then this will do it:
TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
Solution 10 - Android
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
Solution 11 - Android
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"
Solution 12 - Android
Through XML:
android:textStyle="bold"
Through Java:
//Let's say you have a textview
textview.setTypeface(null, Typeface.BOLD);
Solution 13 - Android
The best way to go is:
TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);
Solution 14 - Android
in file .xml, set
android:textStyle="bold"
will set text type is bold.
Solution 15 - Android
4 ways to make Android TextView bold- Full answer is here.
-
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. -
using setTypeface() method
textview2.setTypeface(null, Typeface.BOLD); textview2.setText("TEXTVIEW 2");
-
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));
Solution 16 - Android
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
Solution 17 - Android
You can use this for font
create a Class Name TypefaceTextView and extend the TextView
private static Map
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"
Solution 18 - Android
In my case, Passing value through string.xml worked out with html Tag..
<string name="your_string_tag"> <b> your_text </b></string>
Solution 19 - Android
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);
will set both typface as well as style to Bold.
Solution 20 - Android
In Kotlin we can do in one line
TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
Solution 21 - Android
You can do this
ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);
Solution 22 - Android
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())
}