4

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.

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • I am stuck on the same thing. Can you please guide me on how did you set the color of the shape. I even can't find `setXYZ()`. In the end I had to post a new question all together: http://stackoverflow.com/questions/12464535/setting-gradientdrawable-through-remoteview. Please see if you can help – harshit Sep 17 '12 at 17:57

3 Answers3

16

You can change color of ImageView image in "RemoteViews" by doing this:

remoteviews.setInt(viewid, "setColorFilter", color);
E Player Plus
  • 1,836
  • 16
  • 21
  • This is right answer, I have set drawable on my view with rounded corner and opacity, with this methode I am able to change color while shape with rounded corners stays yay :) – Gordon Freeman Aug 13 '14 at 12:48
  • Works perfectly! wasted so such time just to find this amazing and simple solution (Y). – Anmol Feb 12 '21 at 14:06
3
after modify drawable :



 Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
 remoteViews.setImageViewBitmap(viewId, bitmap) .
Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
3

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>
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Will this allow me to change the shape color for example at run time? I want the user to define the color from the widget configuration activity. – Ranhiru Jude Cooray Nov 13 '11 at 17:57
  • 1
    Yes, via `remoteViews.setXYZ(viewId, "setColor", value)`. You need to set ID of the view in you XML in order to access it. – Peter Knego Nov 13 '11 at 19:03
  • It almost works perfect with one more issue. I set the `src` property to `myshapedrawable` and it works perfectly. So I wanted to change the color of it but I can only set an Id for the `ImageView` and it doesn't have a `setColor` method. How do I call the `setColor` method of the src ? – Ranhiru Jude Cooray Nov 14 '11 at 17:08
  • You mean add an id to the shape in drawables? Doesn't seem to work unfortunately. – Ranhiru Jude Cooray Nov 14 '11 at 17:17
  • Tried to edit the post, the color is incorrect. Should be `#ff74AC23` – Luke Cauthen Sep 09 '16 at 21:46
  • Thanks, this is working! With round corners and the ripple effect. I'm using an ImageButton with a TextView on top. views.setInt(R.id.image_button, "setColorFilter", Color.parseColor("#ff5252")); Make sure to use as background. – einUsername Sep 25 '19 at 08:10