How to create preference headers groups in Android PreferenceActivity?

Erveron picture Erveron · Feb 1, 2013 · Viewed 30.6k times · Source

I am using preference headers to create settings activity using PreferenceActivity. I am trying to divide the headers into categories/groups, like this one (there are categories Wireless & Networks, Device, Personal, ...):

Anyway, even that Android Developers site is about this way of creating preference activity, I couldn't find any way how to create the same preferences activity like they have on the image. The only I managed to do is simple list of preference headers.

The only thing I have found is this, but that works kinda... strange. So that does not seem as an option.

So my question is: How to create PreferenceActivity using preference headers with possibility of dividing headers into categories and with possibility of using master on/off switches?

Some of my code:

preference_headers.xml:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header 
        android:fragment="cz.vse.myevents.activity.SettingsActivity$EventsFragment"
        android:title="@string/settings_events"
        android:icon="@android:drawable/ic_menu_agenda" />
    <header 
        android:fragment="cz.vse.myevents.activity.SettingsActivity$OrganizationsFragment"
        android:title="@string/settings_subscribed_organizations"
        android:icon="@android:drawable/ic_menu_view"  />
</preference-headers>

SettingsActivity:

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

I am not posting fragments resources, think it's unnecessary.

Answer

Pawan Maheshwari picture Pawan Maheshwari · Feb 18, 2013

This is preference category example, you can use preference category and set respective fragment and achieve this, let me know if I misunderstood your case.

Here is sample layout

<PreferenceCategory android:title="Heading1">
        <Preference 
            android:title="title1"
            android:summary="summary1"
            android:key="keyName"/>

       <Preference 
            android:title="title2"
            android:summary="summary2"
            android:key="keyName"/>
</PreferenceCategory>

<PreferenceCategory android:title="Heading2">
        <Preference 
            android:title="title3"
            android:summary="summary3"
            android:key="keyName"/>
</PreferenceCategory>