Ugly PreferenceScreen with default grey on black? |
Lets start with containing those nested PreferenceScreens within your defined Fragment view container. First you will need to create separate xml files for each PreferenceScreen. Like this..
prefs.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <ListPreference android:key="nzb_server_choice" android:title="@string/prefs_server_choice" android:summary="@string/prefs_server_choice_sum" android:entries="@array/nzb_server_choice" android:entryValues="@array/nzb_server_choice" android:defaultValue="@string/sv_name_hellanzb" /> <PreferenceScreen android:title="@string/prefs_hella_settings" android:key="@string/hella_prefs_key" android:fragment="com.brockoli.android.nzbmobilepro.HomeActivity$HellaPrefsFragment"> </PreferenceScreen> </PreferenceScreen>
hella_prefs.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/prefs_hella_settings"> <EditTextPreference android:key="hella_server_url" android:title="@string/prefs_sv_url_title" android:summary="@string/prefs_hella_server_sum" android:defaultValue="@string/prefs_hella_server_default"/> <EditTextPreference android:key="hella_server_port" android:title="@string/prefs_sv_port_title" android:summary="@string/prefs_hella_server_port_sum" android:defaultValue="@string/prefs_hella_server_port_default"/> <EditTextPreference android:key="hella_server_password" android:title="@string/prefs_hella_server_pw_title" android:summary="@string/prefs_hella_server_pw" android:password="true"/> </PreferenceScreen>
Take note of how I defined my PreferenceScreen for hella settings in prefs.xml. You need to specify android:key as well as android:fragment. This way you can key in on the specific PreferenceScreen the user selects from your code and handle it as a Fragment. Now for the code...
public static class PrefsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.prefs); } @Override public boolean onPreferenceTreeClick(PreferenceScreen prefScreen, Preference pref) { super.onPreferenceTreeClick(prefScreen, pref); if (pref.getKey().equals(getActivity().getResources().getString(R.string.hella_prefs_key))) { // Display the fragment as the main content. getFragmentManager().beginTransaction().replace(R.id.details, new HellaPrefsFragment()) .addToBackStack(null).commit(); return true; } return false; } } public static class HellaPrefsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.hella_prefs); } }
Top level PreferenceScreen |
Finally, you should handle the back button correctly so the user can navigate back up through your preferences. To do this, simple add the parent PreferenceFragment to the fragment backstack. To do that you simple call the addToBackStack method of the transaction after replacing the parent fragment.