1

I try to set an Image to an ImageView in my Widget, but instead of picture I got an empty screen

here's layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<ImageView 
    android:id="@+id/stage"
  android:layout_width="match_parent"
 android:layout_height="match_parent"

    />
</RelativeLayout>

here 's code :

public class MyWidget extends AppWidgetProvider {

      Bitmap bitmap;
      Bitmap bckgrnd;
      Bitmap over;

      @Override
      public void onEnabled(Context context) {
        super.onEnabled(context);

 RemoteViews widgetView = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        bckgrnd=BitmapFactory.decodeResource(context.getResources(), R.drawable.backgrnd);
        over=BitmapFactory.decodeResource(context.getResources(), R.drawable.human);

        bitmap=bckgrnd.copy(Bitmap.Config.ARGB_8888, true);;
        Canvas c=new Canvas(bitmap);
        c.drawBitmap(over, 50, 50, new Paint());
        widgetView.setImageViewBitmap(R.id.stage, bitmap);



        Log.d("MyLog", "onEnabled");
      }

    ......
    }

So, I have a Log of "OnEnabled" in my LogCat which means that this code has been executed well, but ImageView remains empty. If I set any image in layout xml - it shows that image, which means xml set right, but code doesn't change this Image in ImageView.

I tried to do this in onUpdate() but it didn't work either. What can I do to change this Image in ImageView?

Lyubov Alekseeva
  • 209
  • 1
  • 4
  • 10
  • Have you called `appWidgetManager.updateAppWidget` method after you set the image? – hoomi Aug 31 '14 at 08:37
  • appWidgetManager.updateAppWidget(new ComponentName(context, MyWidget.class), widgetView); - is this right? I call it from onUpdate() but nothing happens – Lyubov Alekseeva Aug 31 '14 at 08:55
  • how about `public void partiallyUpdateAppWidget (int appWidgetId, RemoteViews views)`? It feels to me from your code that when you change the image you forget to notify the AppWidgetManager of the changes – hoomi Aug 31 '14 at 09:03
  • well, I'm trying to do everything with AppWidgetProvider - isn't that possible? – Lyubov Alekseeva Aug 31 '14 at 10:06
  • How large is the image (width x height, in pixels)? – CommonsWare Aug 31 '14 at 10:23

2 Answers2

2

I think its late now.. but just to help anyone who is still looking.

For me, this worked..

// Change widget image, and update current app widget.
widgetView.setImageViewBitmap(R.id.stage, bitmap);
AppWidgetManager.getInstance(context).updateAppWidget(COMPONENT_NAME, widgetView);

And, COMPONENT_NAME is

COMPONENT_NAME = new ComponentName(context, MyWidget.class);

This will update every widget of your application.. if you want just the widget that is clicked to be updated, then pass its widgetId in place of COMPONENT_NAME in updateAppWidget().

Ankit Bansal
  • 1,801
  • 17
  • 34
  • http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int) – CrandellWS Feb 13 '16 at 11:26
  • This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead. – CrandellWS Feb 13 '16 at 11:27
  • Thanks!! I shared to a couple other posts since there were two similar posts with no answer which came up before this one. Hopefully it helps people. – Mira_Cole Aug 14 '22 at 02:07
1

I encountered similar problem like you, except I call rv.setImageViewResource in subsequent calls from onUpdate.

My solution is that you must set initial image for ImageView in xml file, then you can change the pic for ImageView at runtime; if you didn't specify the initial pic in xml, then setImageViewResource dosen't work, ImageView shows nothing.

Update:

now setImageViewResource works well no matter ImageView has initial src in xml or not, it's wired though.

梁梁梁
  • 41
  • 4