Skip to content Skip to sidebar Skip to footer

Using Linkify.addlinks Combine With Html.fromhtml

I have a TextView that gets it's data set by calling this: tv.setText(Html.fromHtml(myText)); The string myText contains partially formatted html data. For example, it might have

Solution 1:

It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.

Use this code to get it work:

publicstatic Spannable linkifyHtml(String html, int linkifyMask) {
    Spannedtext= Html.fromHtml(html);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableStringbuffer=newSpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        intend= text.getSpanEnd(span);
        intstart= text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}

Solution 2:

You can try this one:

First set the text in to your TextView.

tv.setText(myText);

Convert the links with Linkify

Linkify.addLinks(tv, Linkify.ALL);

and finally replace the text with Html.fromHtml but using the Linkified text from your EditText.

tv.setText(Html.fromHtml(tv.getText().toString()));

Post a Comment for "Using Linkify.addlinks Combine With Html.fromhtml"