Android: how to create per-item settings activity? - android

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.

Related

Preference with in-line options

I've created a PReference Screen which lets the user manage a series of elements marked by himself as "favorites". I create a Preference category then fill it up with options dynamically, taking them from a sqlite database. This is a screencap:
And this is a screen mockup of what I want to achieve:
That is, I want for every preference item to add an in-line remove option that allows users to directly remove favorites.
The problem is I don't know how to do this, and I've seen no similar examples. Is it even possible?
Android provides an easy means to implement preferences screen, but standard android ListPreference does not provide an easy way to display an image for each item if the list. To add the images to the Android ListPreference, we need to use a custom xml attribute
Here is some example that may help you
http://www.cmwmobile.com/index.php?option=com_content&view=article&id=4&Itemid=12
How to add icons to Preference
To delete prefrence from prefrence screen you you can refer this Link : How to determine which preference item from custom layout button click

How can I create a layout like this?

Two tiles SORT and REMINDER and having Sub Item as shown in image.
I have tried this by using Listview but nothing made up like this.
That is easy with Shared Preferences, take a look at this tutorial
In brief: for all Preference Category you have the possibility to add a title, in your case SORT and REMINDER.
You need to use PreferenceCategory to acheive this.
The Settings screen usually makes use of the SharedPreferences and you could use a PreferenceActivity to do it or you can handle all the interaction manually.
The preference category helps you to categorize all the preference settings into various groups, so as to make it easy for the user to understand which setting is for which functionality.
An excerpt from the documentation :
Used to group Preference objects and provide a disabled title above
the group.

How can I dynamically create an Android Preference?

A mockup is below that probably explains better than words. Essentially, I want a list where items can be added/removed dynamically by the user, and each item has configurable settings screen.
So there are two keys here:
Adding to the main preferences screen
Starting an
activityForResult when an item is pressed. This activity will show
another preferences view (sliders, checkboxes, etc) where the user
can manipulate these and then return the new values to be stored in
a data structure.
Image:
I would suggest heading down the road of Fragments - specifically PreferenceFragment: http://developer.android.com/reference/android/preference/PreferenceFragment.html
Why I think this will work well for you:
Furthermore, the preferences shown will follow the visual style of
system preferences. It is easy to create a hierarchy of preferences
(that can be shown on multiple screens) via XML. For these reasons, it
is recommended to use this fragment (as a superclass) to deal with
preferences in applications.
Your question is a little bit vague, but probably this is best solved by storing the user's data in a database (and using standard CursorAdapter and CursorLoader instances to show this data to the user) rather than trying to force everything into the Preferences framework. The CursorAdapter is optimized for dealing with arbitrarily large result sets, while PreferenceActivity and friends really work better with a fixed set of data.
The Preferences stuff is designed to be easy to implement for its specific use case, but if your use case falls out of that scope — and it sounds like it does — it's going to be a hassle to squeeze your data into a preferences model.
If you just like the Preferences UI, you can of course peek at the Android source code to see how it is implemented while still letting your own logic drive a variant of that UI.
Actually creating the preference screens dynamically is easy. You can do it in code (search the API Demos sample app for PreferenceFromCode.java) or by expanding an XML file that you can write (PreferencesFromXml.java). What's going to be hard is coming up with a sensible UI and storage back-end for the user to compose and store these dynamic preference collections.

Android Preferences - changing the order in which they appear

I'm trying to create a hierarchy of classes which start with a PreferenceActivity.
The idea is the base class contains the 'About Me' button, then the next class implements the generic preferences for games/apps/wallpapers and then the next class adds the the specifics for each actual Game/App.
The idea works well BUT because I'm 'adding' preferences from XML at each level, they appear with the most generic (lowest level) ones first (e.g. my 'About' button).
Ideally I'd like them to be the other way up - so that specific preferences appear at the top of the list and generic ones at the bottom - is there any way of achieving this (I've read through the docs on PreferenceFragments and the new PreferenceHeaders and even that doesn't seem to offer anything?)
Is there any programmatic way to move PreferenceCategorys or PreferenceScreens around within the overall order??
Essentially when I'm using
addPreferencesFromResource(R.xml.clock_settings);
I'd like to have a
addPreferencesFromResourceAtBottom(R.xml.clock_settings);
I'm getting good at answering my own questions here :)
The solution lies in the android:order XML parameter - using that with Preference (or PreferenceGroup or a sub-class of it) will sort stuff into the right sequence on screen!
You could also change this dynamically with setOrder() but it won't reorder the Preferences if they've already been added to a PreferenceGroup.

Possible to use the ListPreference modal view in a ListActivity?

I am trying to put together a modal box with a scrollable list of checkable items and an OK and Cancel button at the bottom for the user to select filters from. It seems the simplest way to do things like this in Android is to reuse API components (widgets, layouts, etc) and the closest one I can find to this looks to be the ListPreference, which basically does exactly what I want (I can even work with storing the data in SharedPreferences).
The problem is that I'm not launching this modal box from a PreferenceActivity, but rather will be launching it from either of two activities: a ListActivity and a MapActivity. If a ListPreference would be possible here, that'd be really convenient, but otherwise any help getting me in the right direction would be great.
Thanks!
Nick
Try an AlertDialog, supplying it with your ListAdapter via AlertDialog.Builder.

Categories

Resources