PreferenceActivity , PreferenceFragment and headers (Part 3)
In previous posts we described how to use Preference Activity,Preference Fragment and Preference Header.
As we have seen, the example described in Scenario 3 doesn't work on older device (API Level<11).
The Preference Fragment and associated Preference Headers are not supported on Android platforms prior to Android 3.0 (API level 11). As a result, if you want to create applications with headers that support devices running on both pre- and post-Honeycomb devices you have same alternatives:
PreferenceFragment and Headers with older versions
As the first alternative:
And that’s all! Just execute the app in Android emulator or real device and see following output.
Single-pane layout in older device with "headers" :
As alternative , you need to implement separate Preference Activities to support both, and launch the appropriate Activity at run time, as shown
You can get code from GitHub:
As we have seen, the example described in Scenario 3 doesn't work on older device (API Level<11).
The Preference Fragment and associated Preference Headers are not supported on Android platforms prior to Android 3.0 (API level 11). As a result, if you want to create applications with headers that support devices running on both pre- and post-Honeycomb devices you have same alternatives:
- you need to create an additional preferences XML file that uses basic
elements that behave like the header items - you need to implement separate Preference Activities to support both, and launch the appropriate Activity at run time
PreferenceFragment and Headers with older versions
As the first alternative:
- Step 1: Define the preferences_headers_scenario3.xml
We can use the same file used in scenario3. This file lists each settings group and declares which fragment contains the corresponding list of settings.The file is placed in the res\xml folder.
- Step 2: Define additional preferences_headers_legacy_scenario4.xml
Instead of opening a new PreferenceScreen, each of theelements sends an Intent to the PreferencesActivityABSScenario4.
We have to identify which preference file to load, and we can do it with intent's action.
- Step 3: Modify PreferenceActivity
The only thing left to do is callonBuildHeaders()
only when running on Androd 3.0 or higher in our PreferenceActivityABSScenario4 and calladdPreferencesFromResource()
in older version.public class PreferencesActivityABSScenario4 extends SherlockPreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //LEGACY METHOD FOR OLDER DEVICES -------------------------------------- String action = getIntent().getAction(); int preferenceFile_toLoad=-1; //Manage single fragment with action parameter if (action != null && action.equals(Constants.SETTING_UPDATE)) { preferenceFile_toLoad=R.xml.preference_update; }else if (action != null && action.equals(Constants.SETTING_DISPLAY)) { preferenceFile_toLoad=R.xml.preference_display; }else if (action != null && action.equals(Constants.SETTING_NOTIFY)) { preferenceFile_toLoad=R.xml.preference_notify; } else{ //Inflate main preference file for Android<3.0 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // Load the legacy preferences headers preferenceFile_toLoad=R.xml.preference_headers_legacy_scenario4; } } if (preferenceFile_toLoad>-1){ addPreferencesFromResource(preferenceFile_toLoad); } //-------------------------------------------------------------------- } /** * Populate the activity with the top-level headers. */ @Override public void onBuildHeaders(List target) { loadHeadersFromResource(R.xml.preference_headers_scenario3, target); } }
And that’s all! Just execute the app in Android emulator or real device and see following output.
Single-pane layout in older device with "headers" :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMp2McK42LvoiKDdpyQL2xqPVm-bLulirrRBj9LHnFWDVLvyNQKnVfERUiyhoZrP1YrU885CwH15dghfuXqdmkop3KY6kvtl0rp-cIMHr1Ie5NnQU7DLDXk7kbU3uCSiXcYCr0wiVbxyKY/s320/SC20130203-224120.gif)
As alternative , you need to implement separate Preference Activities to support both, and launch the appropriate Activity at run time, as shown
- Step 1: Use preferences_headers_scenario3.xml and PreferenceActivityABSScenario3
- Step 2:Define alternative preference xml file and PreferenceActivity
public class OtherPreferencesActivity extends PreferenceActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ddPreferencesFromResource(R.xml.otherPreferences); } } - Step 3: Launch the appropriate Activity
Class c = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? OtherPreferenceActivity.class : PreferenceActivityABSScenario3.class; Intent i = new Intent(this, c); startActivityForResult(i, SHOW_PREFERENCES);
You can get code from GitHub:
Comments
Post a Comment