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>
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);
Try using setImageViewResource()
remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With