Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PreferenceScreen/PreferenceActivity to configure home screen widgets

Tags:

android

I'm creating an appwidget, where the user should be able to configure it before it is added to the home screen. To me, PreferenceScreen/PreferenceActivity seems perfect for the task, but the intention behind these classes seem to be configuring an actual application. I've searched, but cannot find any documentation or tutorials which covers this question.

Is it possible to configure a widget using these classes, or is the only alternative to configure it through an ordinary view?

Thanks for any answers!

like image 538
hanspeide Avatar asked Dec 07 '22 03:12

hanspeide


2 Answers

You certainly can use a PreferenceActivity for this purpose. If you set it up as the android:configure activity in your widget configuration, though, you will have to do a bit of fancy footwork to then get your widget updated.

like image 91
CommonsWare Avatar answered Mar 09 '23 00:03

CommonsWare


commonsWare: https://github.com/commonsguy/cw-advandroid/blob/master/AppWidget/LoremWidget/res/xml/widget_provider.xml your example does not use a config-activity at all and thus certainly no PreferencesActivity.

As far as PreferencesActivities go, it does NOT work to setResult(RESULT_CANCELED); in onCreate(9 and then setResult(...) in onBackPressed. There are wired NullPointerExceptions deep in Launcher.java (line 88X). However it does work do Provider.updateAppWidget() and setResult(RESULT_OK, resultValue); in onCreate and then handle further updates in onBackPressed.

The following snipped may help too:

onCreate() {
CheckBoxPreference dark = (CheckBoxPreference)findPreference("xyz");
dark.setChecked(false);
...
onBackPressed() {
CheckBoxPreference dark = (CheckBoxPreference)findPreference("xyz");
boolean checked = dark.isChecked();

...

like image 37
Marcus Wolschon Avatar answered Mar 08 '23 23:03

Marcus Wolschon