Defining Preferences in XML
Although you can instantiate new Preference objects at runtime, you should define your list of settings in XML with a hierarchy of Preference objects. Using a XML file to define your collection of settings are preferred because the file provides an EASY-TO-READ structure T Hat ' s simple to update. Also, your app ' s settings is generally pre-determined, although you can still modify the collection at runtime.
Each Preference subclass can is declared with a XML element that matches the class name, such as <CheckBoxPreference> .
You must save the XML file in the res/xml/ directory. Although can name the file anything you want, it ' s traditionally named preferences.xml . You usually need only one file, because branches in the hierarchy (that's open their own list of settings) are declared Usin G Nested instances of PreferenceScreen .
Note: If you want to create a multi-pane layout for your settings and then you need separate XML files for each fragment.
The root node for the XML file must is a <PreferenceScreen> element. Within this element is the where you add each Preference . Each child you add within the <PreferenceScreen> element appears as a single item in the list of settings.
For example:
<?XML version= "1.0" encoding= "Utf-8"?><Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android"> <checkboxpreferenceAndroid:key= "Pref_sync"Android:title= "@string/pref_sync"android:summary= "@string/pref_sync_summ"Android:defaultvalue= "true" /> <listpreferenceandroid:dependency= "Pref_sync"Android:key= "Pref_syncconnectiontype"Android:title= "@string/pref_syncconnectiontype"Android:dialogtitle= "@string/pref_syncconnectiontype"android:entries= "@array/pref_syncconnectiontypes_entries"android:entryvalues= "@array/pref_syncconnectiontypes_values"Android:defaultvalue= "@string/pref_syncconnectiontypes_default" /></Preferencescreen>
In this example, there ' s A and CheckBoxPreference a ListPreference . Both items include the following three attributes:
-
-
android:key
-
-
This attribute was required for preferences that persist a data value. IT Specifies the unique key (a string) the system uses when saving this setting ' s value in the
SharedPreferences .
The only instances in which this attribute are not required are when the preference are a PreferenceCategory or PreferenceScreen , or the pref erence specifies an Intent -invoke (with-an <intent> element) or a-to Fragment -display (with an android:fragment attribute).
-
-
android:title
-
This
-
provides a user-visible name for the setting.
-
-
android:defaultValue
-
This
-
specifies the initial value, the system should set in the
SharedPreferences file. You should supply a default value of settings.
For information on all other supported attributes, see the Preference (and respective subclass) documentation.
Figure 2. Setting categories with titles.
1. the category is specified by the <PreferenceCategory> element.
2. The title is specified with the android:title attribute.
When your list of settings exceeds about ten items, you might want to add titles to define groups of settings or display th OSE groups in a separate screen. These options is described in the following sections.
Creating setting groups
If you present a list of ten or more settings, the users may have difficulty scanning, comprehending, and processing them. You can remedy this by dividing some or all of the settings to groups, effectively turning one long list into multiple s Horter lists. A Group of related settings can be presented in one of the ways:
- Using titles
- Using Subscreens
You can use one or both of these grouping techniques to organize your app ' s settings. When deciding which to use and how to divide your settings, you should follow the guidelines in Android Design ' s settings Guide.
Using titles
If you want to provide dividers with headings between groups of settings (as shown in Figure 2), place each group Preference of Objects inside a PreferenceCategory .
For example:
<Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android"> <preferencecategoryAndroid:title= "@string/pref_sms_storage_title"Android:key= "Pref_key_storage_settings"> <checkboxpreferenceAndroid:key= "Pref_key_auto_delete"android:summary= "@string/pref_summary_auto_delete"Android:title= "@string/pref_title_auto_delete"Android:defaultvalue= "false"... /> <PreferenceAndroid:key= "Pref_key_sms_delete_limit"android:dependency= "Pref_key_auto_delete"android:summary= "@string/pref_summary_delete_limit"Android:title= "@string/pref_title_sms_delete"... /> <PreferenceAndroid:key= "Pref_key_mms_delete_limit"android:dependency= "Pref_key_auto_delete"android:summary= "@string/pref_summary_delete_limit"Android:title= "@string/pref_title_mms_delete" ... /> </preferencecategory> ...</Preferencescreen>
Using Subscreens
If you want to place groups of settings into a subscreen (as shown in Figure 3), place the group of Preference objects inside a PreferenceScreen.
Figure 3. Setting Subscreens. <PreferenceScreen>the element creates an item and when selected, opens a separate lists to display the nested settings.
For example:
<Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--opens a subscreen of settings - <PreferencescreenAndroid:key= "Button_voicemail_category_key"Android:title= "@string/voicemail"android:persistent= "false"> <listpreferenceAndroid:key= "Button_voicemail_provider_key"Android:title= "@string/voicemail_provider" ... /> <!--opens another nested Subscreen - <PreferencescreenAndroid:key= "Button_voicemail_setting_key"Android:title= "@string/voicemail_settings"android:persistent= "false"> ... </Preferencescreen> <ringtonepreferenceAndroid:key= "Button_voicemail_ringtone_key"Android:title= "@string/voicemail_ringtone_title"Android:ringtonetype= "Notification" ... /> ... </Preferencescreen> ...</Preferencescreen>
Using intents
In some cases, you might want a preference item to open a different activity instead of a settings screens, such as a Web B Rowser to view a Web page. To invoke an when Intent the user selects a preference item, add an element as a child of the the <intent> corresponding <Preference> el Ement.
For example, here's how can I use a preference item to open a Web page:
<android:title= "@string/prefs_web_page"> < android:action= "Android.intent.action.VIEW" android:data= "http// www.example.com "/></Preference>
You can create both implicit and explicit intents using the following attributes:
| Android:action |
The action to assign, as per the setAction() method. |
| Android:data |
The data to assign, as per the setData() method. |
| Android:mimetype |
The MIME type to assign, as per the setType() method. |
| Android:targetclass |
The class part of the component name, as per the setComponent() method. |
| Android:targetpackage |
The package part of the component name, as per the setComponent() method. |
Android Preferences (2) define a preference XML for your app