Android Preferences (5) The Preferences screen displays multiple groupings, and each group also has an interface

Source: Internet
Author: User

Using Preference Headers

In rare cases, you might want to design your settings such that the first screens displays only a list of subscreens (such a s in the system Settings apps, as shown in Figures 4 and 5). When your ' re developing such a design for Android 3.0 and higher, you should use a new "headers" feature in Android 3.0, in stead of building Subscreens with nested PreferenceScreen elements.

To build your settings with headers, you need to:

    1. Separate each group of settings into separate instances of PreferenceFragment . That's, each group of settings needs a separate XML file.
    2. Create an XML headers file This lists each settings group and declares which fragment contains the corresponding list of S Ettings.
    3. Extend the PreferenceActivity class to host your settings.
    4. Implement the onBuildHeaders() callback to specify the headers file.

A great benefit to using this design are that PreferenceActivity automatically presents the Two-pane layout shown in Figure 4 when Runni Ng on large screens.

Even if your application supports versions of Android older than 3.0, you can build your application PreferenceFragment -pane presentation on newer devices when still supporting a traditional multi-screen hierarchy on older devices (see the section on supporting older versions with preference headers).

Figure 4. Two-pane layout with headers.
1. The headers is defined with an XML headers file.
2. Each group of settings are defined by a, that's PreferenceFragment specified by a element in the headers file.

Figure 5. A handset device with setting headers. When a item is selected, the associated PreferenceFragment replaces the headers.

Creating the headers file

Each group of settings in your list of headers are specified by a single element inside a root <preference-headers> element. For example:

<?XML version= "1.0" encoding= "Utf-8"?><preference-headersxmlns:android= "Http://schemas.android.com/apk/res/android">    <Headerandroid:fragment= "Com.example.prefs.settingsactivity$settingsfragmentone"Android:title= "@string/prefs_category_one"android:summary= "@string/prefs_summ_category_one" />    <Headerandroid:fragment= "Com.example.prefs.settingsactivity$settingsfragmenttwo"Android:title= "@string/prefs_category_two"android:summary= "@string/prefs_summ_category_two" >        <!--Key/value pairs can be included as arguments for the fragment. -        <ExtraAndroid:name= "Somekey"Android:value= "Someheadervalue" />    </Header></preference-headers>

android:fragmentwith the attribute, each header declares an instance of this PreferenceFragment should open when the user selects the header.

The <extras> element allows you to pass key-value pairs to the fragment in a Bundle . The fragment can retrieve the arguments by calling getArguments() . You might pass arguments to the fragment for a variety of reasons, but one good reason are to reuse the same subclass of for each group and use the argument to specify which preferences XML file the fragment should load.

For example, here's a fragment that can is reused for multiple settings groups, when each header defines an <extra> argument With the "settings" key:

 Public Static classSettingsfragmentextendspreferencefragment {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); String Settings= Getarguments (). getString ("Settings"); if("Notifications". Equals (Settings))        {Addpreferencesfromresource (R.xml.settings_wifi); } Else if("Sync". Equals (Settings))        {Addpreferencesfromresource (R.xml.settings_sync); }    }}
Displaying the Headers

To display the preference headers, your must implement the onBuildHeaders() callback method and call loadHeadersFromResource() . For example:

 Public class extends preferenceactivity {    @Override    publicvoid onbuildheaders (listtarget) {        Loadheadersfromresource (r.xml.preference_headers, target);}    }

When the user selects a item from the list of headers, the system opens the associated PreferenceFragment .

Note: When using preference headers, your subclass of PreferenceActivity doesn ' t need-implement onCreate() the method, because the only Requir Ed task for the activity was to load the headers.

Supporting older versions with preference headers

If your application supports versions of Android older than 3.0, can still use headers to provide a Two-pane layout wh En running on Android 3.0 and higher. All your need to does is create a additional preferences XML file that uses basic <Preference> elements that behave like the header Items (to is used by the older Android versions).

Instead of opening a new PreferenceScreen , however, each of the elements sends an to the that <Preference> Intent specifies which PreferenceActivity prefer ence XML file to load.

For example, here's an XML file for preference headers that's used on Android 3.0 and Higher ( res/xml/preference_headers.xml ):

<preference-headersxmlns:android= "Http://schemas.android.com/apk/res/android">    <Headerandroid:fragment= "Com.example.prefs.SettingsFragmentOne"Android:title= "@string/prefs_category_one"android:summary= "@string/prefs_summ_category_one" />    <Headerandroid:fragment= "Com.example.prefs.SettingsFragmentTwo"Android:title= "@string/prefs_category_two"android:summary= "@string/prefs_summ_category_two" /></preference-headers>

And here is a preference file, provides the same headers for versions older than Android 3.0 ( res/xml/preference_headers_legacy.xml ):

<Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android">    <PreferenceAndroid:title= "@string/prefs_category_one"android:summary= "@string/prefs_summ_category_one"  >        <IntentAndroid:targetpackage= "Com.example.prefs"Android:targetclass= "Com.example.prefs.SettingsActivity"android:action= "Com.example.prefs.PREFS_ONE" />    </Preference>    <PreferenceAndroid:title= "@string/prefs_category_two"android:summary= "@string/prefs_summ_category_two" >        <IntentAndroid:targetpackage= "Com.example.prefs"Android:targetclass= "Com.example.prefs.SettingsActivity"android:action= "Com.example.prefs.PREFS_TWO" />    </Preference></Preferencescreen>

Because support <preference-headers> for is added in Android 3.0, the system calls in onBuildHeaders() your if running on PreferenceActivity Androd 3.0 o R higher. In order to load the ' legacy ' headers file ( preference_headers_legacy.xml ), you must check the Android version and, if the version is older than A Ndroid 3.0 ( HONEYCOMB ), call to addPreferencesFromResource() load the legacy header file. For example:

@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); ...    if(Build.VERSION.SDK_INT <Build.version_codes. Honeycomb) {//Load The Legacy preferences headersAddpreferencesfromresource (r.xml.preference_headers_legacy); }}//called only on Honeycomb and later@Override Public voidOnbuildheaders (listtarget) {Loadheadersfromresource (r.xml.preference_headers, target);}

The only thing left and do are handle the that's passed into the Intent activity to identify which preference file to load. s O Retrieve the intent's action and compare it to known action strings so you ' ve used in the preference XML ' s <intent> Tags:

Final StaticString Action_prefs_one = "Com.example.prefs.PREFS_ONE";. .. @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); String Action=getintent (). Getaction (); if(Action! =NULL&&action.equals (Action_prefs_one))    {Addpreferencesfromresource (r.xml.preferences); }    ...    Else if(Build.VERSION.SDK_INT <Build.version_codes. Honeycomb) {//Load The Legacy preferences headersAddpreferencesfromresource (r.xml.preference_headers_legacy); }}

Beware that consecutive calls addPreferencesFromResource() to would stack all the preferences in a single list so is sure that it's only called O nCE by chaining the conditions with else-if statements.

Android Preferences (5) The Preferences screen displays multiple groupings, and each group also has an interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.