Search code examples
androidpreferencefragment

Divider between category in PreferenceFragment


I'm trying to achieve Settings app look, which means I'm looking for solution to add divider between categories.

Settings

So I thought I found solution, but unfortunately it's not working for me. As was suggested I should add empty preference with layout:

</PreferenceCategory>
    <Preference
        android:title="Test"
        android:summary="Summary"/>

</PreferenceCategory>

<Preference layout="@layout/divider_preference" />

<PreferenceCategory
    android:title="Category"/>

Here is divider:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="10dp"
              android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/shadow_bottom"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/shadow_top"/>

</LinearLayout>

But what I get is just empty preference:

My

So how could I fix that?


Solution

  • Try with this:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
            android:layout="@layout/divider_preference"
            android:title="Category1">
    
        <Preference
            android:title="Test"
            android:summary="Summary"/>
    
        </PreferenceCategory>
    
        <PreferenceCategory
            android:layout="@layout/divider_preference"
            android:title="Category2">
    
        <Preference
            android:title="Test1"
            android:summary="Summary1"/>
    
        </PreferenceCategory>
    </PreferenceScreen>