Misagh Emamverdi
Misagh Emamverdi

Reputation: 3674

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.

Upvotes: 3

Views: 89

Answers (1)

Gujarat Santana
Gujarat Santana

Reputation: 10564

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

Upvotes: 1

Related Questions