5

I am trying to convert a html String and set it to a TextView but I couldn't do it exactly.

Here is my String, "Hello, %1$s! You have <b>%2$d new messages</b>"

I am using textview.setText(Html.fromHtml(myString)); which produces me an output with Html Tags instead of plain text. Can anyone help me with this?

Any help is much appreciated. Thanks

Andro Selva
  • 53,910
  • 52
  • 193
  • 240

5 Answers5

15

Refer to this question.

try:

textView.setText(Html.fromHtml(myString), TextView.BufferType.SPANNABLE);
Community
  • 1
  • 1
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
7

This may be helpful to you:

Spanned marked_up = Html.fromHtml(myString);
textview.setText(marked_up.toString(),BufferType.SPANNABLE);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
2

Try use this version of setText and use SPANNABLE buffer type

HighFlyer
  • 1,615
  • 2
  • 15
  • 22
0

A bit late but extend TextView and override SetText like so:

public override void SetText(ICharSequence text, BufferType type)
{
    base.SetText(Html.FromHtml(text.ToString()), BufferType.Spannable);
}

Then you can just use your TextView instead of the regular one in your axml.

PmanAce
  • 4,000
  • 2
  • 24
  • 29
0

even later... Html.fromHtml is deprecated now:

textView.setText(
    Html.fromHtml(model.getValue(), Html.FROM_HTML_MODE_COMPACT), 
    TextView.BufferType.SPANNABLE);

Html.FROM_HTML_MODE_COMPACT worked well for me, explore the other flags for different HTML parsing behaviour.

mir
  • 371
  • 2
  • 5