33

Here is the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/modal_list_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="7.5dp"
    android:orientation="vertical" >

    <TextView
                android:id="@+id/title_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="@string/alert_title_small"
                android:textColor="@color/alert_dialog_text_color"
                android:textSize="@dimen/alert_dialog_title_font_size"
                android:textStyle="bold" />

</RelativeLayout>

Although the Graphical Layout shows bold text, on the device it's not. Is it a device thing or what?

Update:

For people who keeps on asking for the ful XML, here it is updated. A simple relative layout. And here is the Java Code:

this.titleTextView.setText(this.modalListState.title);

Things to think about: Could it be due to the device typefaces? Does anyone have Galaxy S3 to confirm? Could it be the activity style? Here is the one used for the whole app:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="TransparentActivity" >
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFullscreen">false</item>
</style>

The TransparentActivity style is used for the concerned activity.

Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79

10 Answers10

42

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!

Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79
23

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)

Community
  • 1
  • 1
Nilesh Patel
  • 419
  • 1
  • 7
  • 15
  • 1
    Still not working...it seems like there is something that overrides this style although I am sure I am not doing it by code – Abdalrahman Shatou Jun 12 '13 at 10:14
  • i was already using this method, but i was passing the current typeface from my button as the first parameter and experiencing the same issue as OP. Changing it to null solved the problem for me. – wainy Jun 29 '17 at 14:01
  • Thank you, passing null (instead of the TextView's typeface) as the first parameter also resolved my issue. – Michael Peterson Aug 17 '17 at 11:37
10

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

  • This was exactly the case for me - after commenting out the call to `CalligraphyConfig.initDefault(..)`, the xml attribute `android:textStyle="bold"` was working. Good insight, thanks – Gene Bo Jul 01 '19 at 23:36
7

Most of the answers are correct.

You can also use the so called : SpannableString

You can use it this way :

String bold = "yes !";
String notBold = "no ";
SpannableString text = new SpannableString ( notBold + bold );
text.setSpan ( new StyleSpan ( 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 !

Leeeeeeelo
  • 4,333
  • 3
  • 34
  • 44
4

I faced a similar problem with the escaped character <. You have to use &lt; instead. For instance:

<string name="greetings">&lt;b>From Russia with love&lt;/b></string>

It's written on the official site.

Paul
  • 4,160
  • 3
  • 30
  • 56
BarbosSergos
  • 314
  • 1
  • 8
3

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.
skpal202
  • 1,217
  • 1
  • 9
  • 9
2

Try using HTML tags in your strings.xml file. This is how you can do it, android string.xml reading html tags problem

To do it programmatically,

TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));
Community
  • 1
  • 1
Aman Gautam
  • 3,549
  • 2
  • 21
  • 25
1

Could you please post all of your java and xml codes for us to gather more information about your issue?

It is possible that the issue is not in the TextView you are showing us but so far the

android:textStyle="bold"

seems to work on your graphical layout.

Gaurav
  • 103
  • 11
  • @A.Shatou what is the value of this.modalListState.title in your java code? Can you please post that code since you are setting the textview to it? – Gaurav Jun 12 '13 at 17:40
1

There must be a bold / italic / underlined (i.e. target) version of the font. The system does not magically create it for you.

Thus ensure that the target variant of your font is included.

Ujjwal Singh
  • 4,908
  • 4
  • 37
  • 54
  • I only have my blogger_sans_light font on assets folder, calling textView.setTypeface(textView.getTypeface(), Typeface.BOLD); magically creates the font in bold format. – Ezequiel Adrian Jan 07 '22 at 02:08
0

Create custom theme for or all device layout. and set into your textview.

I think problem is text will be very slow so you can get any effect.

If you have test this issue then change font size up to 40sp and run what happen?

User
  • 4,023
  • 4
  • 37
  • 63
Harshid
  • 5,701
  • 4
  • 37
  • 50