Douglas Roos
Douglas Roos

Reputation: 613

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

Upvotes: 8

Views: 7859

Answers (2)

Tri Mueri Sandes
Tri Mueri Sandes

Reputation: 148

 String mIsi = (String) bundle.get("isi");
        isi.setText(Html.fromHtml(mIsi, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                Picasso.get()
                        .load(source).into(imageView, new Callback() {
                    @Override
                    public void onSuccess() {
                        drawable = imageView.getDrawable();

                        drawable.setBounds(0, 0,
                                drawable.getIntrinsicWidth(),
                                drawable.getIntrinsicHeight());
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.v("test pic","err"+e.getMessage());
                    }
                });

                imageView.setVisibility(View.GONE);

                return drawable;
            }
        }, null));

Upvotes: 2

Libin
Libin

Reputation: 17085

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));

Upvotes: 12

Related Questions