Skip to content Skip to sidebar Skip to footer

Android Change Widget Background Image

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

Solution 1:

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"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/backgroundImage"android:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/some_background"android:scaleType="fitXY"/><RelativeLayoutandroid:gravity="center"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid: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 widgetAppWidgetManagerappWidgetManager= AppWidgetManager.getInstance(context);
RemoteViewsviews=newRemoteViews(getPackageName(), R.layout.widget_layout);

views.setImageViewResource(R.id.backgroundImage, R.drawable.some_other_background);

appWidgetManager.updateAppWidget(widgetId, views);

Solution 2:

Try using setImageViewResource()

remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);

Thanks

Solution 3:

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

Post a Comment for "Android Change Widget Background Image"