android - multiple instances of widget with separated working clickable ImageView -


i trying make imageview widget, when user click on widget, changes image.

but problem when have multiple instances, , click on 1 of them, changes image in instances.

i have tried code in here: updating multiple instances of app widget in android didnt work.

this 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 button event         remoteviews.setonclickpendingintent(r.id.widgetimageview, buildbuttonpendingintent(context));          // request 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 remoteviews use appwidget instances supplied appwidget provider.

you want use method: updateappwidget(int appwidgetid, remoteviews views) set remoteviews use specified appwidgetid.

to need keep track of widget's id

@override public void onupdate(context context, appwidgetmanager appwidgetmanager,         int[] appwidgetids) {     final int n = appwidgetids.length;      (int = 0; < n; i++) {         // individual id widget         int appwidgetid = appwidgetids[i];          remoteviews remoteviews = new remoteviews(context.getpackagename(), r.layout.widget_main);          // pass appwidgetid click handler         remoteviews.setonclickpendingintent(r.id.widgetimageview, buildbuttonpendingintent(context, appwidgetids[i]));          // pass appwidgetid update method         pushwidgetupdate(context, remoteviews, appwidgetids[i]);      } } 

then update 2 methods use appwidgetid

public static void pushwidgetupdate(context context, remoteviews remoteviews, int appwidgetid) {     appwidgetmanager manager = appwidgetmanager.getinstance(context);     manager.updateappwidget(appwidgetid, remoteviews);  }  public static pendingintent buildbuttonpendingintent(context context, int appwidgetid) {     intent intent = new intent();     // put appwidgetid update intent     intent.putextra(appwidgetmanager.extra_appwidget_id, widgetid);     intent.setaction("widgetutils.widget_update_action");      return pendingintent.getbroadcast(context, appwidgetid, intent,             pendingintent.flag_update_current); } 

finally, update receiver take appwidgetid , use update call

@override public void onreceive(context context, intent intent) {      if(intent.getaction().equals("widgetutils.widget_update_action")){         int appwidgetid = intent.getintextra(appwidgetmanager.extra_appwidget_id, appwidgetmanager.invalid_appwidget_id);         updatewidgetpictureandbuttonlistener(context, int appwidgetid);     }  }   private void updatewidgetpictureandbuttonlistener(context context, int appwidgetid) {      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, appwidgetid);  } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -