How to load the same screen preference in the detail PreferenceFragment

Lukap picture Lukap · Feb 5, 2013 · Viewed 7.6k times · Source

I am trying to do something like the settings preferences in android for tablet.

When I click the "More" the the "Wireless & Networks" preferences screen is displayed on the right fragment, than if I touch the "VPN" , in the same fragment the "VPN" preferences screen is opened.

How can I do that ?

this is my preference xml

<PreferenceScreen android:title="Title A">
        <PreferenceScreen android:title="TITLE B">
            <PreferenceCategory
                android:title="category">
                <ListPreference android:key="list"
                    android:title="list" android:entries="@array/list_vals"
                    android:entryValues="@array/list_vals1"
                    android:defaultValue="1" android:dialogTitle="list title"
                    android:negativeButtonText="cancel" />
            </PreferenceCategory>
        </PreferenceScreen>
</PreferenceScreen>

The problem is I do not want the nested preference screen with title B to be loaded on the whole screen I just want to be loaded in the place of the right fragment...

NOTE: that I used this documentation to create my example so far http://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders

enter image description here

enter image description here

EDIT*

this is my activity

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }
}

I do not have an layout, so how can I know what is my master fragment id and what is my detail fragment id ?

Answer

S.D. picture S.D. · Feb 5, 2013

Short example, define a PreferenceActivity, and provide a headers file for main categories:

Class SettingsActivity:

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onBuildHeaders(List<Header> target) {
        super.onBuildHeaders(target);
        loadHeadersFromResource(R.xml.headers,target);
    }
}

File /res/xml/headers.xml:

<?xml version="1.0" encoding="utf-8"?>

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:title="Preference Header"
        android:fragment="com.example.SettingsDemo.PrefFragment"/>
</preference-headers>

Create a PreferenceFragment as referenced in android:fragment above, provide its own preferences file:

class PrefFragment:

public class PrefFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }
}

File /res/xml/prefs.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="Preferences">
 <SwitchPreference android:title="Preference" />
    <PreferenceScreen android:title="Sub Preferences"
        android:fragment="com.example.SettingsDemo.SubPrefFragment"/>
 </PreferenceCategory>
</PreferenceScreen>

Create a child PreferenceFragment as referenced in android:fragment above, provide its own preferences file too:

Class SubPrefFragment:

public class SubPrefFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.sub_prefs);
    }
}

File /res/xml/sub_prefs.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Sub Preferences">
        <CheckBoxPreference android:title="Sub Preference"/>
    </PreferenceCategory>
</PreferenceScreen>

That's it. The PreferenceActivity will automatically show all the child fragments.

Results:

Main PreferenceFragment : enter image description here

Replaced by sub PreferenceFragment , under same header: enter image description here