Search code examples
androidandroid-layoutandroid-fragmentsandroid-preferencesandroid-styles

Set Title style for PreferenceCategory in PreferenceFragmentCompat


I want to set title style for my preference fragment screen V14.
This is what I want:
enter image description here

I have followed
Custom PreferenceCategory Headings
I did manage to get the same screen but with PreferenceFragment!!
How can I do it for PreferenceFragmentCompat V14??


Here is my Code

Style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:listSeparatorTextViewStyle">@style/PreferenceStyle</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

<style name="PreferenceStyle" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/colorDialogPop</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/text_medium</item>
    <item name="android:padding">@dimen/padding_large</item>
    <item name="android:layout_marginLeft">@dimen/margin_medium</item>
    <item name="android:background">@color/colorAccent</item>
</style>


preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/heading_general"
    android:layout="@layout/settings_text">

    <SwitchPreference
        android:padding="@dimen/padding_medium"
        android:title="@string/enable_push" />

    <SwitchPreference
        android:padding="@dimen/padding_medium"
        android:title="@string/send_email" />

</PreferenceCategory>

<PreferenceCategory android:title="@string/heading_account"
    android:layout="@layout/settings_text">

    <EditTextPreference
        android:padding="@dimen/padding_medium"
        android:title="@string/email" />

    <EditTextPreference
        android:padding="@dimen/padding_medium"
        android:title="@string/name" />

</PreferenceCategory>
</PreferenceScreen>

setting_text.xml for layout of title text

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/PreferenceStyle"
>

</TextView>

All I get is this:
enter image description here


Solution

  • SOLVED
    I added the Linear Layout around the Text View and the background gets visible in my choice of color.
    Here is the code.

    settings_text.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <TextView
        android:id="@android:id/title"
        style="@style/CustomPreferenceCategoryText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
    


    style.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="preferenceTheme">@style/PreferenceStyle</item>
    </style>
    
    <style name="PreferenceStyle" parent="@style/PreferenceThemeOverlay.v14.Material">
        <item name="preferenceCategoryStyle">@style/CustomPreferenceCategory</item>
    </style>
    
    <style name="CustomPreferenceCategory" parent="@style/Preference.Category">
        <item name="android:layout">@layout/settings_text</item>
    </style>
    
    <style name="CustomPreferenceCategoryText" parent="@android:style/Widget.TextView">
        <item name="android:textColor">@color/colorDialogPop</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">@dimen/text_medium</item>
        <item name="android:padding">@dimen/padding_large</item>
        <item name="android:background">@color/colorAccent</item>
    </style>
    


    preference.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory android:title="@string/heading_general">
    
        <SwitchPreference
            android:padding="@dimen/padding_medium"
            android:title="@string/enable_push" />
    
        <SwitchPreference
            android:padding="@dimen/padding_medium"
            android:title="@string/send_weekly_email" />
    
    </PreferenceCategory>
    
    <PreferenceCategory android:title="@string/heading_account">
    
        <EditTextPreference
            android:padding="@dimen/padding_medium"
            android:title="@string/email" />
    
        <EditTextPreference
            android:padding="@dimen/padding_medium"
            android:title="@string/name" />
    
    </PreferenceCategory>
    </PreferenceScreen>