I am trying to make ImageView widget, when the user click on the widget, it changes the image.
But the problem is when i have multiple instances, and click on one of them, it changes the image in all instances.
I have tried the code in here: Updating multiple instances of App Widget in Android but didnt work.
This is my code:
package com.appwidget.test;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// super.onUpdate(context, appWidgetManager, appWidgetIds);
// initializing widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
// register for button event
remoteViews.setOnClickPendingIntent(R.id.widgetImageView, buildButtonPendingIntent(context));
// request for widget update
pushWidgetUpdate(context, remoteViews);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("WidgetUtils.WIDGET_UPDATE_ACTION");
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
package com.appwidget.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyWidgetIntentReceiver extends BroadcastReceiver {
public static int clickCount = 1;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("WidgetUtils.WIDGET_UPDATE_ACTION")){
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
remoteViews.setImageViewResource(R.id.widgetImageView, getImageToSet());
remoteViews.setOnClickPendingIntent(R.id.widgetImageView, MyWidgetProvider.buildButtonPendingIntent(context));
MyWidgetProvider.pushWidgetUpdate(context, remoteViews);
}
private int getImageToSet() {
if (clickCount == 5)
{
clickCount = 0;
}
int drawables = R.drawable.ui1 + clickCount;
clickCount++;
return drawables;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appwidget.test" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
<receiver
android:name="MyWidgetIntentReceiver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="WidgetUtils.WIDGET_UPDATE_ACTION" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_main"
android:minWidth="110dp"
android:minHeight="40dp"
android:updatePeriodMillis="0"
android:resizeMode="horizontal|vertical">
<!--android:previewImage="@drawable/"-->
<!-- n = Number of cells -->
<!--70 × n − 30-->
</appwidget-provider>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/widgetImageView"
android:src="@drawable/ui1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The method updateAppWidget(ComponentName provider, RemoteViews views) sets the RemoteViews to use for all AppWidget instances for the supplied AppWidget provider.
You want to use this method: updateAppWidget(int appWidgetId, RemoteViews views) which set the RemoteViews to use for the specified appWidgetId.
To do this you will need to keep track of the widget's id
Then update the two methods to use the appWidgetId
Finally, update the receiver to take the appWidgetId extra and use it for the update call