Remove selection in bold from a text field without changing other attributes

I use setTypeface to set the text in bold (or in italics or other font attributes)

 TextView tv = findViewById(R.id.label); ... tv.setTypeface(null,Typeface.BOLD); ... 

How to remove only bold attribute without changing other attributes that could be set so far?

+4
source share
2 answers
 tv.setTypeface(null,Typeface.NORMAL); 

This will restore the style to normal without changing color or size.

But you cannot mix bold / italic / underlined text this way. If you specify BOLD, all text will be in bold. If you want to mix text style, I suggest using HTML to style the text, and then use the following code.

 tv.setText(Html.fromHtml(yourStringAsHtml)); 
+9
source

this piece of code deleted the old Typeface setTypeface (null, Typeface.NORMAL);

To save the old you have to call

setTextViewStyle (textView, isBold);

 private void setTextViewStyle(TextView view, boolean isBold){ if (view == null) return; // if old typeface is null create new Typeface bold or def Typeface oldTypeface = view.getTypeface() != null ? view.getTypeface() : (isBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); view.setTypeface( Typeface.create(oldTypeface, isBold ? Typeface.BOLD : Typeface.NORMAL) ); } 
0
source

Source: https://habr.com/ru/post/1487716/


All Articles