Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set typeface of text view to Typeface.NORMAL doesn't have any effect, why?

I have listeners for ExpandableListView for group collapsed and expanded:

// There is a text view on group layout, no problem there.
    @Override
    public void onGroupCollapse(int groupPosition) {
        // this callback is triggered, however, once the textView is BOLD by onGroupExpanded, the textView is not set back to normal, seems this line of code does nothing...WHY?
        textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
    }

    @Override
    public void onGroupExpand(int groupPosition) {
        // it works fine
        textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    }

As you can see above, I have a textView in group layout, when group is expanded, I bold the textView, if collapsed, I try to set it back to unbold by Typeface.NORMAL.

Both callbacks are triggered correctly, however, once the textView is BOLD by onGroupExpanded(...) callback, the textView is not set back to NORMAL when onGroupCollapse(...) is triggered afterwards. Seems the line of code in onGroupCollapsed(...)does nothing...WHY?

(Again, onGroupCollapse(...) is triggered. No problem there!)

like image 692
Leem Avatar asked Dec 13 '17 16:12

Leem


People also ask

What is text Style in android studio?

The android:textStyle attribute can be used to put emphasis on the text. The possible values are: normal , bold , italic . You can also specify bold|italic .

Which API can be used to change the text font?

Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio.

How do you change text to bold on android?

Android: Tap and hold the text you're entering in the text field, then choose Bold, Italic, or More . Tap More to choose Strikethrough or Monospace. iPhone: Tap the text you're entering in the text field > Select or Select All > B_I_U. Then, choose Bold, Italic, Strikethrough, or Monospace.


1 Answers

If you use android:fontFamily on this TextView then I am pretty sure

textView.setTypeface(null, Typeface.NORMAL);

will change to the default font. Another way to do this is:

textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL));

The original problem is that TextView's setTypeface with the style oddly skips over some stuff if the style is NORMAL. I've tested this on one version, and it works.

like image 181
androidguy Avatar answered Oct 12 '22 00:10

androidguy