January 2013
Intermediate to advanced
408 pages
9h 23m
English
The previous recipe shows how to use the SharedPreferences object to store key/value pairs programmatically from within your Android application. If you want to let the users edit the values of these data directly, you can use a preference activity.
Instead of programmatically creating the key/value pairs to store in the SharedPreferences object, you can define the various keys in an XML file. To do so, create a new xml folder under the res folder in your project. Within this folder, create an XML file and name it something meaningful, such as myapppreferences.xml. Populate the myapppreferences.xml file as follows:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="Category 1" > <CheckBoxPreference android:defaultValue="false" android:key="checkboxPref" android:summary="True or False" android:title="Checkbox" /> </PreferenceCategory> <PreferenceCategory android:title="Category 2" > <EditTextPreference android:defaultValue="[Enter a string here]" android:key="editTextPref" android:summary="Enter a string" android:title="Edit Text" /> <RingtonePreference android:key="ringtonePref" android:summary="Select a ringtone" android:title="Ringtones" /> <PreferenceScreen android:key="secondPrefScreenPref" android:summary="Click here to go to the second ...Read now
Unlock full access