Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 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>
like image 777
user577732 Avatar asked Dec 01 '22 02:12

user577732


2 Answers

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);
like image 189
Vladimir Sizov Avatar answered Dec 03 '22 15:12

Vladimir Sizov


Try using setImageViewResource()

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

Thanks

like image 36
Sunil Kumar Sahoo Avatar answered Dec 03 '22 15:12

Sunil Kumar Sahoo