Recipe 10.2 Creating a Preference Screen
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.
Solution
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 ...
Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.