Reputation: 12989
I am starting to experiment with app widgets, and at present I've only really taken the stock code provided by google in the Android Studio -- i.e. using the context menu to create the necessary files and code for the app widget.
When enabling the Configuration Activity as part of this example setup, this produces an example widget that simply displays the text that the user inputs in the configuration activity.
But should the configuration activity open up again by default when the user presses on the widget, so that they can change the text? Or does one have to set up an explicit event listener or something of that sort to open up the configuration activity again? If so, any clues or pointers as to how to achieve this would be most welcome.
Upvotes: 1
Views: 1436
Reputation: 426
@puntofisso's answer is what I've spent hours looking for(!) apart from the last line that @Richard Barraclough found issue with.
There should also be declared a String:
String APPWIDGET_CONFIGURE = "android.appwidget.action.APPWIDGET_CONFIGURE";
(the same as found in the AndroidManifest.xml):
<activity android:name=".AppWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
With the final line being more along the lines of:
configIntent.setAction(APPWIDGET_CONFIGURE + currentWidgetId);
With that I now have a widget I can reconfigure!
Upvotes: 1
Reputation: 393
To do what you ask you need to register a configuration intent that triggers the configuration activity.
In your onUpdate method, you would do the following:
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.yourwidgetlayout);
Intent configIntent = new Intent(context, ConfigurationActivity.class);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, currentWidgetId);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, currentWidgetId, configIntent, 0);
views.setOnClickPendingIntent(R.id.yourfavouritelayout, configPendingIntent);
configIntent.setAction(ACTION_WIDGET_CONFIGURE + Integer.toString(currentWidgetId));
Where R.id.yourlayout is the ID of the layout that needs to trigger the configuration activity when touched. As a note, I would do this on a small "settings" button, rather than on the whole widget, if the widget is supposed to have controls on it.
Upvotes: 3