Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Bold from textview without changing other attributes

I use setTypeface to set a text bold (or italics, or other of the typeface attributes)

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

How do I remove only the bold attribute, without changing other attributes that might have been set so far?

like image 648
ilomambo Avatar asked Jun 23 '13 13:06

ilomambo


People also ask

How do I make TextView bold in specific text?

style. StyleSpan(Typeface. BOLD), start, end, Spannable.

How do I turn off bold text on Android?

On your device, open the Settings app. Display size and text. Turn Bold text on or off.

How do I make text bold in TextView android programmatically?

Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .

How do you change bold font on Android?

Double tap the text you want to format. Tap Format, then choose a formatting option like bolding, italics, or changing the font color.


4 Answers

tv.setTypeface(null,Typeface.NORMAL);

This would set the style back to normal without changing color or size.

But you can't mix bold/italic/underline text this way. If you specify BOLD, all of the text will be bold. If you want to mix the style of the text I suggest using HTML to style the text, and then use the following code.

tv.setText(Html.fromHtml(yourStringAsHtml));
like image 107
Flynn81 Avatar answered Oct 17 '22 04:10

Flynn81


just create a new Typeface can make bold back to normal

tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);
tv.invalidate();
like image 24
djzhao Avatar answered Oct 17 '22 05:10

djzhao


this chunk of code removed the old Typeface setTypeface(null,Typeface.NORMAL);

For keeping the old you should 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)
    );
}
like image 1
Vahe Gharibyan Avatar answered Oct 17 '22 05:10

Vahe Gharibyan


like me, if you want to preserve the custom font you have set to the TextView and just remove BOLD attribute, you can try

tv.getPaint().setFakeBoldText(false)

Doing following removed the custom font I had set to the TextView.

tv.setTypeface(null,Typeface.NORMAL);
like image 1
Dhunju_likes_to_Learn Avatar answered Oct 17 '22 03:10

Dhunju_likes_to_Learn