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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With