rbcc
rbcc

Reputation: 2482

Best way to implement a complex preference screen?

For my app, I have a fairly complex set of configuration options that user can pick from. I'm currently using a PreferenceActivity as my user interface for these options and the options are stored using shared preferences. As an example of some of the settings I have to accommodate:

However, my feeling is PreferenceActivity doesn't work well with settings like the above because:

I could write my own Preference classes for configuring the lists, but I find these really laborious to implement compared to implementing a typical View and I've still got to deal with the storage issues.

My plan was:

Does this plan seem reasonable? I'm concerned I'm not doing things the Android way, but it seems to me that shared preferences and PreferenceActivity aren't suited to my needs here.

Upvotes: 4

Views: 1298

Answers (1)

Brandon
Brandon

Reputation: 1373

Since I began in Android, I have always created my own preference activities. It seems really difficult because there is not much documentation on the internet on how to do it, but it is, in fact, really quite simple. As you said, it gives you a lot more freedom in deciding exactly how your UI looks and acts. Just in case you're wondering how to do preferences yourself, here's a little simple snippet:

public class myprefs extends Activity{
private static final String PREFS_XML = "prefs_xml";
private static final String PREF_1 = "pref_1";

String preference;

private SharedPreferences preferences = null;
public void loadPrefs(){
    preferences = this.getSharedPreferences(PREFS_XML, Activity.MODE_PRIVATE);
    preference = preferences.getString(PREF_1, "default value");
}
}

That's pretty much how simple it is to get your own preferences. To set them you use

preferences.edit().putString(PREF_1, "hello!").commit();

That can be put into an onClick, onItemSelected, or any other 'event' you want to put it into. I made 'preferences' a class wide instance so that I can access it anywhere in the class without having to re-instantiate it. I hope this helps you out a little bit. As a specific answer to your specific question, I would think your plan is perfectly reasonable.

Upvotes: 1

Related Questions