Android TextView in BOLD format and plain text

I carefully searched for SO but did not get an answer to my question.

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

The text contains heading and steps and plain text. I want the title and steps to be in bold and remain in plain text.

I can do this using different ones, but how can I do this in the same TextView.

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

+6
source share
6 answers

in the string 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 work

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

And conclusion

enter image description here

+12
source

Use 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 a more stylish look check out the link @ http://blog.stylingandroid.com/archives/177

+19
source

You can format it like in HTML: let 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))); 

This will create a spannable string and set it to a TextView.

+2
source

enter this line in res-> string.xml

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

Now you can use it every time you need this thing.

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

It’s best to work and work.

+2
source

TextView support for SpannableStrings . You can make your own String or format your string in html and then set it using tv.setText(Html.fromHtml(yourString));

+1
source

Use this instead:

Declare textview first

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

Then set the text to the desired line

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

Finally, use a font for text viewing

tv1.setTypeface(null, Typeface.BOLD);

And you are done.

0
source

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


All Articles