2

I have a widget with collection (this is important!). It looks like this:

Android widget screenshot

I based my code on official Android documentation: Android App Widgets.

So for widget I use collection with custom Objects inside StackView collection. This collection is handled by StackRemoteViewsFactory (implements RemoteViewsService.RemoteViewsFactory). And as you can see each item has three ImageButtons and one TextView.

I know how to add onClick behavior for whole RemoteView item. This described in official docs.

But I need four onClick behaviors for each of my Views (buttons and textview).

My question: Is this possible to have different views onClicks for each StackView item in widget with RemoteView collection?

Now I don't see any possibilities how to this :(


UPDATE:

I want to have something similar with onClicks as in this question: Processing more than one button click at Android Widget. But a solution from that question doesn't work for widget with collection of RemoteViews because of differences in widgets implementations.

Community
  • 1
  • 1
dGorod
  • 21
  • 4
  • Have u got your answer, because i am facing the same issue. can u look at my question http://stackoverflow.com/questions/15965625/widget-update-issue-while-click-of-next-and-previous-buttton – AndroidDev Apr 12 '13 at 12:33
  • Did you ever solve this? It would be helpful if you could post your solution please. – Dave Jan 24 '16 at 14:57

2 Answers2

1

if you have seen StackView widget sample code, you know there are provider class and service class. In service class, try that add setOnClickFillInIntent to each id of stackview layout with Intent including "command word". and In provider class set setPendingIntentTemplate as sample code. this is important part, In provider class, there is OnReceive(). setPendingIntentTemplate will send Intent including two things that a specific action you set in Provider class and a "comman word" set to each id of layour from service class. then now you know which button users clicked from Stack View widget. If you need further Tips for that, tell me i'll add example code.

KevinRyu
  • 256
  • 4
  • 6
  • 1
    Sorry, it is hardly to understand your English. Please, provide an example code. Maybe it will be more simple. Thank you. – dGorod Aug 31 '12 at 09:23
  • @user1482049 Can u look at my question and tell me what mistake actually i have made in my code http://stackoverflow.com/questions/15965625/widget-update-issue-while-click-of-next-and-previous-buttton – AndroidDev Apr 12 '13 at 12:34
0

So Basically you have to do 2 things to add onClick behavior to a button in StackView/ListView in Android App Widget.

  1. In the getViewAt function of StackRemoteViewsFactory which implements RemoteViewService.RemoteServiceFactory, add a setOnClickFillingIntent() for the id you want to add function. eg.

     RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget__fan);
     Bundle extras = new Bundle();
     extras.putInt(TestWidget.EXTRA_ITEM, position);
     Intent fillInIntent = new Intent();
     fillInIntent.putExtras(extras);
     rv.setOnClickFillInIntent(R.id.fan_status, fillInIntent); // id of button you want to add a onClick function to.
    
  2. In your onUpdate function of WidgetProvider class which extends AppWidgetProvider, add a setPendingIntentTemplate(). eg.

     Intent toastIntent = new Intent(context, TestWidget.class);
     toastIntent.setAction(TestWidget.act1);
     toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
     PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
     rv.setPendingIntentTemplate(R.id.lview, toastPendingIntent);
    

Now, the action has been set for that particular button (R.id.fan_status in my case). You can add its functionalities in onRecieve() function. eg.

public void onReceive(Context context, Intent intent) {    
    if (intent.getAction().equals(act1)) {
       // Add your functionalities
    }
}