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,136
  • 52
  • 190
  • 238

5 Answers5

14

Refer to this question.

try:

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

This may be helpful to you:

Spanned marked_up = Html.fromHtml(myString);
textview.setText(marked_up.toString(),BufferType.SPANNABLE);
Paresh Mayani
  • 125,853
  • 70
  • 238
  • 294
2

Try use this version of setText and use SPANNABLE buffer type

HighFlyer
  • 1,595
  • 2
  • 14
  • 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
  • 3,608
  • 2
  • 21
  • 28
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
  • 301
  • 2
  • 5