简体   繁体   中英

Android Spanned text is blank on TextView

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?

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

Would you like to try this one?

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

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM