Search code examples
androidandroid-imageviewremoteviewshapedrawable

Call setImageDrawable from RemoteViews


I have done this in an Activity and it works perfectly.

ImageView myImage = (ImageView) findViewById(R.id.myImageView);

ShapeDrawable mDrawable;

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
mDrawable.setIntrinsicWidth(width);
mDrawable.setIntrinsicHeight(height);

myImage.setImageDrawable(mDrawable);

Now I want to do the same thing in a widget (inside onUpdate) and I have to use RemoteViews to access the image.

How do I call the setImageDrawable of an ImageView from RemoteViews? All the remote views methods seems to take a Bitmap.


Solution

  • RemoteViews are built from XML resource descriptors. You can not use code to build them.

    You need to do something like this:

    Create a layout:

    <ImageView android:src="@drawable/myshapedrawable">
    </ImageView>
    

    Then define a new shape drawable named myshapedrawable.xml (in res/drawables folder):

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
        <size android:width="200" android:height="100"/>
        <solid android:color="0xff74AC23"/>
    </shape>