14

I'm working on a ListView widget where I want the user to be able to launch a activity when the ListView is clicked. I haven't been able to find any sort of tutorial on this so I'm wondering if anyone could point me in the right direction or perhaps share some code. I want to launch the same activity regardless of which ListItem is clicked so that's not a problem.

All help is appreciated!

SweSnow
  • 17,504
  • 10
  • 36
  • 49

2 Answers2

44

Have a look here and scroll to the subheading Adding behavior to individual items.

You need to make sure you call both setPendingIntentTemplate() from your AppWidgetProvider and setOnClickFillInIntent() from your RemoteViewsService.RemoteViewsFactory implementation.

For example:

public class Widget extends AppWidgetProvider {

    // ...

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for(int i = 0; i < appWidgetIds.length; i++){

            RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent startActivityIntent = new Intent(context, myActivity.class);
            PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            widget.setPendingIntentTemplate(R.id.list_view, startActivityPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);

            // ...
    }
}

public class WidgetAdapter implements RemoteViewsService.RemoteViewsFactory {

    // ...

    @Override
    public RemoteViews getViewAt(int position) {

    RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_row);

        Intent fillInIntent = new Intent();
        fillInIntent.putExtra(Widget.EXTRA_LIST_VIEW_ROW_NUMBER, position);
        widgetRow.setOnClickFillInIntent(R.id.list_view_row, fillInIntent);

        // ...

        return widgetRow;
    }
}

There is a more conclusive example in the StackWidget sample which is in the SDK samples, although I found it somewhat difficult to find (see here for directions). It creates an intent to show a Toast message, but it uses the same code.

AtaerCaner
  • 691
  • 7
  • 12
William Carter
  • 1,287
  • 12
  • 19
  • it seems it gets all the data in the ListView so when going to the activity it doesn't return the correct extra you putted. How to solve this? – natsumiyu Sep 20 '16 at 06:05
  • could you help me? here's mine http://stackoverflow.com/questions/39587137/how-to-get-the-clicked-item-from-listview-of-a-widget-android thank you. – natsumiyu Sep 20 '16 at 06:41
-10

To launch any Activity you create an Intent and then call startActivity on that intent. Check Intent and startActivity and I am sure you will able to do it yourself.

Run2
  • 1,839
  • 22
  • 32
  • 1
    Of course I know how to launch a activity the ordinary way. I'm wondering about how you do it with a ListView widget since the ordinary PendingIntent won't work. – SweSnow Jan 13 '13 at 08:33