Custom PreferenceCategory Headings Custom PreferenceCategory Headings android android

Custom PreferenceCategory Headings


You should take a look at Preference.Category style:

<style name="Preference.Category">    <item name="android:layout">@android:layout/preference_category</item>   <item name="android:shouldDisableView">false</item>   <item name="android:selectable">false</item></style>

Let's take a look at preference_category.xml file:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. --><TextView xmlns:android="http://schemas.android.com/apk/res/android"    style="?android:attr/listSeparatorTextViewStyle"    android:id="@+android:id/title"/>

So you need to create custom theme that extends default android Theme and override listSeparatorTextViewStyle value with ListHeader style. And then apply this theme to Activity that extends PreferenceActivity .


Here is how you can do it.

First, in your styles.xml add next code:

<style name="PreferenceListHeader"        parent="@android:style/Widget.TextView.ListSeparator">    <item name="android:textColor">#000000</item>    <item name="android:textStyle">bold</item>    <item name="android:textSize">12sp</item>    <item name="android:background">#cccccc</item>    <item name="android:paddingTop">6px</item>    <item name="android:paddingBottom">6px</item>    <item name="android:paddingLeft">12px</item></style><style name="Theme.Custom" parent="@android:style/Theme">    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>               </style>

Then in your AndroidManifest.xml add theme to your preference acitivity:

 <activity android:name=".MyPreferencesActivity"            android:theme="@style/Theme.Custom"            ... > ... </activity>

Here is a screenshot:

enter image description here


@inazaruk gave the answer well enough but since the recent updates, ADT 18 and above, there are some restrictions on the styles giving the error

Error retrieving parent for item: No resource found that matches the given name '@android:style/Widget.TextView.ListSeparator'.

See this link for reason of the problem and the solution. Since this post doesn't provide a code for understanding i am providing my code here

 <style name="Widget.TextView.ListSeparator" parent="@android:style/Widget.TextView">    <item name="android:layout_width">match_parent</item>    <item name="android:layout_height">wrap_content</item>    <item name="android:textStyle">bold</item>    <item name="android:textSize">14sp</item>    <item name="android:gravity">center_vertical</item></style><style name="PreferenceListHeader" parent="Widget.TextView.ListSeparator">    <item name="android:textColor">#000000</item>    <item name="android:textStyle">bold</item>    <item name="android:textSize">18sp</item>    <item name="android:background">#cccccc</item>    <item name="android:paddingTop">6dp</item>    <item name="android:paddingBottom">6dp</item>    <item name="android:paddingLeft">12dp</item></style><style name="PreferenceScreen" parent="android:Theme.NoTitleBar">    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>    <item name="android:background">#F2B1DBF3</item></style>


Styling it as described in inazaruk's answer is simple enough but it only changes the style of the text in the heading, it doesn't offer a way to specify a whole new layout (the style will not apply the layout item). There is, however, a straightforward solution if you extend the class:

public class MyPreferenceCategory extends PreferenceCategory {  public MyPreferenceCategory(Context context) {    super(context);    setLayoutResource(R.layout.yourlayout);  }  public MyPreferenceCategory(Context context, AttributeSet attrs) {    super(context, attrs);    setLayoutResource(R.layout.yourlayout);  }}

and simply use this instead of the original PreferenceCategory when defining your Preferences layout.

Your layout can, of course, have anything you like, including lines above or below the text, different backgrounds, padding, whatever. For instance, this will show a material design colored subtitle with a line above:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_marginTop="6dp"        android:background="?attr/divider_color" />    <TextView        android:id="@+android:id/title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:padding="12dp"        android:textColor="?attr/colorAccent"        android:textSize="14sp"        android:textStyle="bold" /></LinearLayout>