Android Change Widget Background Image
Solution 1
Anyone got any other ideas? Really would like to get this figured out
Edit: ended up setting up different layouts currently have 10 different layouts for 2 different widgets inefficient but working more of a hack around really
Solution 2
Here's a trick that you can do: use ImageView for you background, not "background" property of you View and set scaleType to "fitXY". Like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/some_background"
android:scaleType="fitXY"/>
<RelativeLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="some button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- all your views -->
</RelativeLayout>
</FrameLayout>
Now you can switch your ImageView source during runtime:
//updating current widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_layout);
views.setImageViewResource(R.id.backgroundImage, R.drawable.some_other_background);
appWidgetManager.updateAppWidget(widgetId, views);
Solution 3
Try using setImageViewResource()
remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);
Thanks
user577732
Updated on June 04, 2022Comments
-
user577732 almost 2 years
Been struggling for the past two days to change the background of my widget, based on some if statements (removed right now just want to change the widget background from the class) here is my source below. What's up though, I've changed images before fine such as backgrounds but can not get it to work for my widget thank you. This is my most recent attempt by the way
//Widget Provider Class public class WidgetProvider extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, com.widget.WidgetDialog.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); views.setOnClickPendingIntent(R.id.widget, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); views.setImageViewBitmap(R.id.widget, ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background)).getBitmap()); } } } //Widget Layout XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/widget" android:background="#00000000" android:layout_width="fill_parent" android:layout_height="fill_parent"> </Button> </LinearLayout>
-
user577732 over 12 yearsI tried this in the past as its what made the most sense to me but writing what u suggest errors should be views.setImageViewResource(R.id.widget, R.drawable.widget_background) ; which then also doesn't work so in the xml I set the background to #00000000 but then what I set in my class doesn't show I just have a clear widget any other suggestions? Thanks
-
jamesc almost 12 yearsWorked a treat for me Thank's very much!
-
Ganesh Kanna over 7 years@Vladimir Sizov How to load image from url?