androidexpandablelistviewandroid-typeface

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!)


Solution

  • Use this instead:

    textView.setTypeface(null, Typeface.NORMAL);
    

    you need to put null to drop the other typefaces (in your case Bold). How I understand it one text view can have multiple typesfaces, like for example italic+bold etc. and the first parameter is to indicate if you want to keep those or not. So basically your previous code kept BOLD and added NORMAL, and BOLD won ;-)

    see also related answer and comments here https://stackoverflow.com/a/6200841/2399024