views:

43

answers:

1

I am trying to display images dynamically on a home screen widget. As RemoveViews offer a very limited set of methods to interact with the layout, I'm trying to use HTML to get the same effect. Regular HTML displays ok (<u>,<b> etc) however I can't get images to display. Instead I get a little empty square appear in the TextView.

E.g.

s = "<b>Hello</b> <img src='world'/>";
ImageGetter imgGetter = new ImageGetter(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.textarea, Html.fromHtml(s, imgGetter, null));

The image getter looks like the following:

public class ImageGetter implements Html.ImageGetter
{
    Context c;

    public ImageGetter(Context c)
    {
        this.c = c;
    }
    public Drawable getDrawable(String source) {
        Drawable d = null;
        int resID = c.getResources().getIdentifier(source, "drawable", c.getPackageName());

        d = c.getApplicationContext().getResources().getDrawable(resID);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
}

The code works fine if I am using a regular Activity. I suspect that this is a bug/undocumented feature of android when used with a AppWidgetProvider. I just want some confirmation.

A: 

I am trying to display images dynamically on a home screen widget.

Use ImageView. Update the app widget when you wish to change the image.

CommonsWare