Skip to content Skip to sidebar Skip to footer

Android - Setting Textview To Bold Not Working

Here is the XML:

Solution 1:

Well, I found a silly answer to this problem. I was using a custom font on the device, not the default one. Once I switched to the default one, every things worked as expected!

Solution 2:

there is one method, you can set textview typeface by calling setTypeface method...

 textView.setTypeface(null, Typeface.BOLD_ITALIC);
 textView.setTypeface(null, Typeface.BOLD);
 textView.setTypeface(null, Typeface.ITALIC);

and also refer this link...

Set TextView style (bold or italic)

Solution 3:

The reason TextView not changes to bold when you run your application on mobile device is that you must have used Calligraphy in your activity. So, to change the style to bold all you have to do is write the following code in your java file.

 textView.setTypeface(textView.getTypeface(), Typeface.BOLD);

Good Luck

Solution 4:

Most of the answers are correct.

You can also use the so called : SpannableString

You can use it this way :

Stringbold="yes !";
StringnotBold="no ";
SpannableStringtext=newSpannableString ( notBold + bold );
text.setSpan ( newStyleSpan ( Typeface.BOLD ) , notBold.length () , text .length () , 0 );
myTextView.setText ( text , BufferType.SPANNABLE );

What is nice about the SpannableString is that you can apply mutliple spans, each on different parts of the string ! As you noticed, I applied the bold type face only on a part of the string (you specify the start and end indexes) , and it should look like this :

no yes !

In the method setSpan, you specify the SPAN to apply, the starting index, the ending index, and flags (I always use 0), in that specific order.

You can even apply other spans, like change the text size (use RelativeSizeSpan ), or even color (use ForegroundColorSpan ), and much more !

Here is an example for the color span, that you can achieve in the following manner :

text.setSpan ( new ForegroundColorSpan ( Color.RED) , 0 , notBold .length () , 0 );

And now, the first part of the string (containing the word no) will be displayed in red !

Solution 5:

The font size defined in the dimensions file might not have been picked up during run. Guess you are not running the app in emulator.

Check the following to ensure that its not device thing.

  • ensure the font size is correctly defined for the right dimensions file for the target device metrics
  • Some devices allows changing default text size. Check default text size under 'Settings' in the device.
  • Any style or theme applied in your manifest file for the activity in which the layout is displayed. Try removing the style/theme for a while.
  • try hard coding the font size to even number for e.g. 24sp
  • check the scale property of any of the parent views/layouts.
  • no code is trying the change the font style during runtime.

Post a Comment for "Android - Setting Textview To Bold Not Working"