0

I am using Spanned object to process some HTML tags and showing the resulted text on TextView. But the Text is blank on TextView.

final Spanned output = Html.fromHtml(element.getText(), null,
                    new ElementTagHandler(element));

            textView.setText(output);

In the textView if I set a constant string, Its working as expected

            textView.setText("Hello");  \\ works perfectly 

But when I pass in Hello its showing blank TextView.


        <TextView
                android:id="@+id/view123"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal"/>

Am I missing any flag that needs to be updated?

Sandy K
  • 65
  • 1
  • 1
  • 8
  • Kind of hard to debug this without the text. Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Ryan M Feb 21 '20 at 00:51

2 Answers2

0

You can do it simpler like link

    @SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
    if(html == null){
        // return an empty spannable if the html is null
        return new SpannableString("");
    }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // FROM_HTML_MODE_LEGACY is the behaviour that was used for versions below android N
        // we are using this flag to give a consistent behaviour
        return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
    } else {
        return Html.fromHtml(html);
    }
}

while html variable can consist of a string with html tags such as , etc...

but if you insist you can do something like this link

DemoDemo
  • 372
  • 1
  • 12
0

Would you like to try this one?

textView.setText(Html.fromHtml(element.getText()), TextView.BufferType.SPANNABLE);

I hope this helps.

Genesis
  • 358
  • 2
  • 8