Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create preference headers groups in Android PreferenceActivity?

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.

like image 986
Erveron Avatar asked Sep 10 '25 21:09

Erveron


2 Answers

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>
like image 193
Pawan Maheshwari Avatar answered Sep 12 '25 12:09

Pawan Maheshwari


Seems the best solution is creation of three different blocks of code - one for pre-Honeycomb, one for post-Honeycomb and one for tablets.

Usage of preference headers is effective on tablets only, so they remain on tablets only. No grouping is used here.

Preference headers on post-Honeycomb are kinda useless, so the best is usage of typical PreferenceScreen in a PreferenceFragment. Groups can be made easily by PreferenceCategory.

And finally, for the pre-Honeycomb, the deprecated way without using PrefrenceFragment is the only way.

Sadly there is a lot of code duplication, but the UnifiedPreference library mentioned in the answer by Leandros is buggy - it ignores PreferenceFragment totally so it is useless (at least for me).

like image 20
Erveron Avatar answered Sep 12 '25 10:09

Erveron