Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML tags in string for TextView

Tags:

android

If I put simple HTML formatting tags, such as <b>...</b> into a string resource and display the string in a TextView, the expected formatting is applied. But how can I do this if I build up my own String and display it? If I do something like String str = "This is <b>bold</b>";, the actual tags get displayed -- not the expected bolding.

Do I have to run the string through some other method to cause the tags to be recognized as tags?

like image 294
gordonwd Avatar asked Dec 28 '10 12:12

gordonwd


People also ask

Can I make a TextView clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How can I get HTML text in android?

htmlToTextView. setText(HtmlCompat. fromHtml(htmlText, 0)); In the above code we are taking HTML data from fromHtml() and appending to textview using setText() .

What is Spannable string in android?

Text styling is one of the important aspects when it comes to enhancing the UI of an Android application. In Android, we can change the size, color, weight, style, etc of a text and make the text more attractive and appealing.


1 Answers

You have to use Html#fromHtml

String input = "<b>bold</b>";
myTextView.setText(Html.fromHtml(input));
like image 134
RoflcoptrException Avatar answered Oct 12 '22 19:10

RoflcoptrException