13

I thoroughly searched on SO but didnt get answer of my question.

I want to set a paragraph, I will set it in XML using

The text contains Title and Steps and regular text. I want to make the Title and Steps in bold and rest in normal text.

I can do this by using different 's but how can I do it in the same TextView.

I mean using the same TextView how can I set different attributes for different sentences?

8 Answers8

31

Use a Spannable String

 TextView tv = (TextView) findViewById(R.id.tv);  
 String steps = "Hello Everyone";
 String title="Bold Please!";

 SpannableString ss1=  new SpannableString(title);
 ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0); 
 tv.append(ss1);
 tv.append("\n");
 tv.append(steps);

enter image description here

For more styling check the link @ http://blog.stylingandroid.com/archives/177

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    [Here is the Screenshot](http://image.prntscr.com/image/bbe1b95ceedf437eae54a751986c8641.png) Thanks, this works fine for me! As you can see I could set BOLD for "Title", "Author" and "Tags". – AlexMelw Jan 07 '17 at 01:18
15

in your strings file

<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content  content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content  content content content  content content content</p></p>
]]>
</string>

Then in your activity

        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(Html.fromHtml(getString(R.string.your_text)));

And output

enter image description here

Arun C
  • 9,035
  • 2
  • 28
  • 42
  • In this case we directly use the values in String , so XML involved na? –  Jun 06 '13 at 12:32
  • Tried this but its not considering spaces :

    43
    x 78
    ____
    4

    Even in comments its not taking spaces...there are 3 spaces before 43 and 4
    –  Jun 06 '13 at 15:07
  • should I use &nbsp ? It will work but any other alternative? –  Jun 06 '13 at 15:14
3

You can format it like you would in HTML: let's call this custom_text

<b>Your title here</b>
This is the non-bolded stuff. 

And then load the text using the Html class:

mTextView.setText(Html.fromHtml(getString(R.string.custom_text)));

That will create a spannable string and set it on the TextView.

daniel_c05
  • 11,438
  • 17
  • 60
  • 78
3

please put this string in to res->string.xml

<string name="your_html">
    <![CDATA[p><b>This is bold text</b> rementing is simple text

    ]]>
    </string>

Now you can used whenever you have to require this thing.

tv.setText(Html.fromHtml(getString(R.string.your_html)));

It is bestway and work charm.

Harshid
  • 5,701
  • 4
  • 37
  • 50
1

TextViews support SpannableStrings. You can either make your custom String or format your string in html and then set it with tv.setText(Html.fromHtml(yourString));

AMerle
  • 4,354
  • 1
  • 28
  • 43
1

kotlin Solution : For me, the issue was I had a text set in XML through data binding. I had to remove that. then this started taking effect

val spannableStringBuilder = SpannableStringBuilder(headerText)
    spannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), 0, headerText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spannableStringBuilder.append(" ")
    spannableStringBuilder.append(subText)
    textView.text = spannableStringBuilder
Tejas
  • 433
  • 1
  • 4
  • 17
1

The best way to do this is in the Strings resources.

<string name="sample_string"><![CDATA[<b>Title</b>StackOverflow]]></string>

Notice, the text between bold tag (<b> </b>) will appear in bold. Similarly, you can set other styles. For eg. <i> for italics and <u> for underline.

Hope this helps, good luck!

Akanshi Srivastava
  • 1,160
  • 13
  • 24
0

Instead, use this :

First, declare textView

TextView tv1 = (TextView) getActivity().findViewById(R.id.t1); 

Then set it's text to the string you want

tv1.setText(Html.fromHtml(getString(R.string.my_text)));

Lastly, use set the typeface for your textView

tv1.setTypeface(null, Typeface.BOLD); 

And, you are done.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Aniket Jadhav
  • 166
  • 11