URL:http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials/Preferences
The Basics
- Use to store data for long-term use (keep for multiple launches of activity or application)
- Primary purpose is to store user-specified configuration details
- Can be for a single activity or shared among all activities in an application
- Stored in the form of key-value pairs
4 Steps to Adding Preferences to Your Application
XML File
Create a preferences.xml file in your res/xml/ directory. The root of the preference xml is a PreferenceScreen element. Within the PreferenceScreen element you can put preference definitions ( CheckBoxPreference, DialogPreference, ListPreference, EditTextPreference, RingtonePreference, etc.)
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="@string/checkbox"
android:title="Large Text"
android:summary="Magnify Text on Screen" />
</PreferenceScreen>PreferenceActivity
Create a PreferenceActivity. You set up your preferences with the xml file. Then make them accessible to the user through a PreferenceActivity.
Toggle line numbers
1 public class EditPreferences extends PreferenceActivity {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5
6 addPreferencesFromResource(R.xml.preferences);
7 }
8 }Don’t forget to add your subclass of PreferenceActivity to your AndroidManifest.xml file.
<activity android:name=".PreferenceActivitySubclass"
android:label="@string/app_name">
</activity>Invoke your activity
Invoke the PreferenceActivity such as from a menu option.
Toggle line numbers
1 @Override
2 public boolean onCreateOptionsMenu(Menu menu) {
3 menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit Prefs")
4 .setIcon(R.drawable.misc)
5 .setAlphabeticShortcut('e');
6 menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "Close")
7 .setIcon(R.drawable.eject)
8 .setAlphabeticShortcut('c');
9 return(super.onCreateOptionsMenu(menu));
10
11 }
12
13 @Override
14 public boolean onOptionsItemSelected(MenuItem item) {
15 switch (item.getItemId()) {
16 case EDIT_ID:
17 startActivity(new Intent(this, EditPreferences.class));
18 return(true);
19
20 case CLOSE_ID:
21 finish();
22 return(true);
23 }
24
25 return(super.onOptionsItemSelected(item));
26 }Access stored preferences
Access stored preferences to reflect user choices in your activity (put in onResume() method).
Toggle line numbers
1 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2
3 TextView checkbox = (TextView)findViewById(R.id.textread);
4 if(prefs.getBoolean(getString(R.string.checkbox), false)) {
5 checkbox.setTextSize(20);
6 } else {
7 checkbox.setTextSize(12);
8 }Helpful Links
- Mark Murphy’s The Busy Coder’s Guide to Android Development was a great resource that helped me write this tutorial. You can find excerpts from his preference tutorial here: http://androidguys.com/?tag=android-preferences.
- Android Package Summary: http://developer.android.com/reference/android/preference/package-summary.html
No comments:
Post a Comment