Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show image in textview using Html.fromhtml();

I have a list adapter to show content frm my social network, i need to put inside the text the img tags, i've tried using Html.fromhtml and it's formatting the text but instead of shows the img it shows a gray square.

How can i achieve that? I'v read about ImageGetter but i still don't have it very clear.

Thanks

like image 387
Douglas Roos Avatar asked Apr 22 '14 01:04

Douglas Roos


1 Answers

You can draw an Image in TextView using the Html img tag with Html.ImageGetter. But make sure your image is available in resource drawable folder

Here is a sample , The image will be loaded from the resource.

String htmlText = "Hai <img src=\"ic_launcher\"> Hello";

textView.setText(Html.fromHtml(htmlText, new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
   int resourceId = getResources().getIdentifier(source, "drawable",getPackageName());
   Drawable drawable = getResources().getDrawable(resourceId);
   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            return drawable;
        }
    }, null));
like image 60
Libin Avatar answered Oct 20 '22 05:10

Libin