Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot toggle TextView style dynamically

In my project, I need to toggle TextView style to Normal and Bold. Here is the code:

mTitleTextView.setTypeface(mTitleTextView.getTypeface(), letterItem.isRead() ? Typeface.NORMAL : Typeface.BOLD);

This works good when TextView is not bold. But when current state is bold, it doesn't return to normal state.

mTitleTextView.setTypeface(null, letterItem.isRead() ? Typeface.NORMAL : Typeface.BOLD);

Above line fixes the problem, but I have used custom font and passing null for current typeface removes the font.

like image 331
Misagh Emamverdi Avatar asked Apr 25 '26 14:04

Misagh Emamverdi


1 Answers

After tying for a while this works for me :

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        textView.invalidate();
        if(isCliked){
            isCliked = false;
            Typeface face=Typeface.createFromAsset(getAssets(), "test.ttf");
            textView.setTypeface(face,Typeface.NORMAL);
        }
        else{
            Typeface face=Typeface.createFromAsset(getAssets(), "test.ttf");
            textView.setTypeface(face, Typeface.BOLD);
            isCliked = true;
        }
        Log.i("MainActivity", "onClick: "+isCliked);
    }
});

the typeface will remain the same, and change only bold and normal

like image 80
Gujarat Santana Avatar answered Apr 27 '26 05:04

Gujarat Santana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!