Mridang Agarwalla
Mridang Agarwalla

Reputation: 44978

How can I set an setOnClickPendingIntent for my entire widget layout?

I'd like to set an setOnClickPendingIntent for my entire widget layout but I haven't found a a way to do this. I guess it's something very trivial but I've overlooked it. Currently I'm setting an intent for each of my views in the layout and this makes my code very messy. Here's what I'm doing currently:

remView = new RemoteViews(ctxContext.getPackageName(), R.layout.initial);
Intent ittRetry = new Intent(ctxContext, SettingsActivity.class);
ittRetry.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
ittRetry.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, intInstance);
PendingIntent pitRetry = PendingIntent.getBroadcast(ctxContext, 0, ittRetry, PendingIntent.FLAG_UPDATE_CURRENT);        
remView.setOnClickPendingIntent(R.id.widget_rows, pitRetry);
remView.setOnClickPendingIntent(R.id.widget_message, pitRetry);
wigManager.updateAppWidget(intInstance, remView);

Upvotes: 0

Views: 570

Answers (3)

mars3142
mars3142

Reputation: 2561

My solution for this issue (years after the original question) is to set an id for the master layout. So my layout has a RelativeLayout and this has an id, which I can use for the pending intent.

Upvotes: 0

FredG
FredG

Reputation: 104

You can create a transparent button on top of all your widget views at the end of your layout xml that covers the whole widget (match_parent is important!):

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>

And reference it in your widget code:

[...]
views.setOnClickPendingIntent(R.id.button, pendingIntent);
[...]

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006644

Currently I'm setting an intent for each of my views in the layout and this makes my code very messy

However, that is your only option. You have to call setOnClickPendingIntent() for every widget for which you want to get control in a click.

You are welcome to use a loop, putting your widget IDs in an array and iterating over that array, calling setOnClickPendingIntent() in each pass.

Upvotes: 1

Related Questions