Skip to content Skip to sidebar Skip to footer

Wants To Have Multiple Text Display Styles In A Single Textview

I am working on an android app i want to know how i can make few words in bold regular and few in italic for example i have a textview with a text HELLOO now I want to display the

Solution 1:

Say, you have a TextView namely etx, use the following code:


finalSpannableStringBuildersb=newSpannableStringBuilder("HELLOO");

        finalStyleSpanbss=newStyleSpan(android.graphics.Typeface.BOLD); // Span to make text boldfinalStyleSpaniss=newStyleSpan(android.graphics.Typeface.ITALIC);Span to make text italic
        sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold 
        sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
        etx.setText(sb);

The main advantage of using this approach is that you can format text dynamically.

Solution 2:

<b> is deprecated. use <strong> and <em> instead of <i>.

text1.setText(Html.fromHtml("<strong>bold text</strong> normal text <em>italic text</em> "));

Solution 3:

If you have String in strings.xml then.

<stringname="startup"><b><i>HELL</i></b><i>oo</i></string>

You can use HTML Tags Here inside <string> </string>

Solution 4:

If you want to make it bold use the b tag.If you want it to be italic use i tag.If u want to make a word bold or italic put that single word inside the tag.

<resources><stringname="register"><u><b><i>SignUp</i></b></u></string></resources>

Solution 5:

Stringtext="<html>"+"<b>"+"Hell"+"</b>"+"<i>"+"oo"+"</i>"+"</html>";

Post a Comment for "Wants To Have Multiple Text Display Styles In A Single Textview"