6

I'm trying to make the listrows in my ListView Widget clickable by using the setOnClickFillInIntent method but whenever I click a ListItem nothing happends. Here are some key parts of my code:

Intent i = new Intent();
Bundle extras = new Bundle();

extras.putInt(Resource.WIDGET_PACKAGE, position);
i.putExtras(extras);
row.setOnClickFillInIntent(R.id.widget_layout_parent, i);

This is at the end of getView() in my ViewFactory. Resource.WIDGET_PACKAGE contains my package name and is just like any other key. The position int is from the getView() parameters. R.id.widget_layout_parent is the parent layout in all of the list items.

Here's the end of my WidgetProviders onUpdate()

Intent clickIntent = new Intent(context, ItemListActivity.class);

PendingIntent clickPI = PendingIntent.getActivity(context, 0,
        clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

widget.setPendingIntentTemplate(R.id.widget_layout_parent, clickPI);

appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i],
        R.id.widget_list_view);

Is there something I'm missing or is there anything else you'd like to know?

All help is appreciated! :)

sabadow
  • 5,095
  • 3
  • 34
  • 51
SweSnow
  • 17,504
  • 10
  • 36
  • 49

3 Answers3

22

When you call setPendingIntentTemplate(...), the id should be that of the AdapterView (in this case the ListView). When you call setOnClickFillInIntent(...), the id should be that of the root View of the item layout, whcih is what you are already doing if I read your original post correctly.

public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds) {
    /* ... */
    widget.setPendingIntentTemplate(R.id.appwidget_listview, clickPI);
    /* ... */
}

public RemoteViews getViewAt(int position) {
    /* ... */
    row.setOnClickFillInIntent(R.id.widget_layout_parent, intent);
    /* ... */
    return row;
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Excellent answer! After hours of researching, this was the exact answer I was looking for. – Kamran Ahmed Aug 21 '14 at 09:36
  • Do anyone have full code example of listview onclick in android widget? I followed https://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/ example, but there are no item onclick. I tried to add onclick but it's not working. :( – Mostafa Imran Aug 25 '16 at 04:50
  • @MostafaImran this is covered in detail here: https://developer.android.com/guide/topics/appwidgets/index.html#implementing_collections. If you have a specific problem, open a new question and provide enough code and information so that we can help. – Karakuri Aug 25 '16 at 06:57
4

Above answer by @karakuri works perfect.

Both setPendingIntentTemplate and setOnClickFillInIntent must be used together. One won't work without the other.

sivag1
  • 4,734
  • 3
  • 32
  • 35
4

If your app is targeting Android 12 and above, you might need to update from PendingIntent.FLAG_UPDATE_CURRENT to PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE

Note that FLAG_MUTABLE is important, otherwise the fill-in intent from View Factory won't be updated into the pending intent template.

val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
  PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
} else {
  PendingIntent.FLAG_UPDATE_CURRENT
}
val clickPI = PendingIntent.getActivity(context, 0, clickIntent, flag)
thomasdao
  • 2,927
  • 1
  • 28
  • 24