Using the Android preference framework without PreferenceActivity - android

I have a preferences.xml, containing various settings for my app - their possible values\names, defaults, etc.
It was previously used simply in a PreferenceActivity with addPreferencesFromResource().
Now I am creating a completely customized settings UI, and I want it to have nothing to do with PreferenceActivity.
All I ever want is to have my preference hierarchy from the XML (as PreferenceScreen), so I can use it to construct my own UI.
I don't want Android's list adapter, I don't want the dialogs, etc. Only the data model.
Sadly, I haven't found a nice way to do that.
The only close thing I can think of is extending PreferenceActivity, providing a custom "R.id.list", and setting its visibility to GONE, so no one will see Android's preference UI.
Any good, clean way to accomplish that?

You can accomplish this by using the PreferenceManager directly. First you would call the setDefaultValues which does the loading of your preferences XML resource. Next you can get access to the SharedPreferences by calling getDefaultSharedPreferences and perform any preference changes through the SharedPreferences object. To edit your preferences you will need to get the Editor from your SharedPreferences object. Make sure to call commit on the editor to actually save the changes you have made.

Related

Dynamically create preference screen and sharedpreferences file

I have a preference activity and some list preferences whose options I need to populate dynamically so:
Can I populate these list preferences dynamically or do I need to create the whole thing (activity) dynamically?
If I do create the whole thing manually will the sharepreferences be update with my choices? (getDefaultSharedPreferences) and if not how can I get the choices I made?
Thanks!
If you want to provide settings for your app, you should use Android's Preference APIs to build an interface that's consistent with the user experience in other Android apps.
More information here.

How to create custom preferences?

After long struggle i am posting this question with hope where i will get answer to my question.
I can't able to create custom preferences activity with background image, view bars between every preference,.
As i had designed one custom preferences page but i cant able to make preference cell as button and i cant able to add key values to preferences to make them as sharedpreferences, even i tried with preferencesfragment also, but i couldn't able to make it work.
Please help me from this custom preferences activity.

Android: how to create per-item settings activity?

I have a collection of items which are shown using a ListView.
Each of these items has its own settings. By clicking on one item I want to allow the user to view/change its settings.
What is the best way to do this?
It would be nice if I could use something like PreferenceActivity as this provides a nice layout. Unfortunately the preference mechanism always saves the chosen preferences to a global file (see SharedPreferences).
A workaround might be using multiple files, one per item, but I don't like this solution since the collection is dynamic and I prefer the settings to be local to my objects.
Another workaround might be opening a PreferenceActivity, then reading all the settings and saving them to the appropriate object. This also seems far from elegant.
Finally, I could implement my own activity with a ListView and custom item layouts, but this seems a duplicated work to get the same behavior and style.
Any suggestion is welcome :)
Thank you
I'd do something along the lines of PreferenceActivity, but save the preferences to the items themselves or something associated with them. To display the preferences input, use a dialog Fragment. You can then either pop up the dialog, or display it as a regular Fragment.

Per instance widget preferences VS app preferences

My app is currently using a subclass of PreferencesActivity for its preferences.
I am in the process of adding widgets, where each instance has its own preferences (I also offer multiple widget sizes, which means multiple subclasses of AppWidgetProvider).
The widgets and the app share most of their preferences, the widget actually supporting a few items less.
In the process of writing the widget configurator activity, I am realizing that I need to access a preferences layout which does not exist.
As I understand it, is not possible to access the PreferencesActivity layout in the configurator activity. Does this means that I must create a sepecific preferences layout for the configurator activity ? Do you have any tips on optimizing the overlap between app and widget preferences ?
Your configuration activity does not to have to be a PreferenceActivity. Make it a normal Activity and use the SharedPreferences.Editor class to change the preferences the widget uses.

Responding to preference updates in Android

I am calling a PreferenceActivity from another activity and then updating the application state (ie: changing the font size) on onActivityResult, based on the preference changes.
I was thinking it would be better to put the state update logic in the PreferenceActivity. That way I don't have the duplicate the logic in each activity that calls the PreferenceActivity.
What's the best or correct way to do this?
Have any Activity (or other component) that cares about preference changes register a preference change listener via registerOnSharedPreferenceChangeListener(). Then, when the preferences change by any means, they will find out about it and can react accordingly.
The PreferenceActivity should handle all the preference setting. Your other activities should read what those settings are when they run and adjust themselves accordingly.

Categories

Resources