1

I have an issue styling the (PreferenceScreen) in my project.

How can I change the colors of of (Title) & (Summary) texts.

No luck after trying for hours with different methods, I must be doing something wrong

SettingsFragment.Java

public class SettingsFragment extends Fragment {


    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().getFragmentManager().beginTransaction()
                .replace(R.id.flContent, new SettingsPreference())
                .commit();
    }

    public static class SettingsPreference extends PreferenceFragment {

        public SettingsPreference() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);
        }

    }

}

fragment_setings.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/app_background"
    android:clickable="true"
    android:focusable="true"
    tools:context="com.companyv.app.fragments.SettingsFragment">

</FrameLayout>

settings.xml

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

    <PreferenceCategory
        android:key="pref_key_storage_settings"
        android:title="First">

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="pref_key_auto_delete"
            android:summary="Sub 1 summary"
            android:title="Sub 1" />

        <Preference
            android:dependency="pref_key_auto_delete"
            android:key="pref_key_sms_delete_limit"
            android:summary="Sub 2 summary"
            android:title="Sub 2" />

        <Preference
            android:dependency="pref_key_auto_delete"
            android:key="pref_key_mms_delete_limit"
            android:summary="Sub 3 summary"
            android:title="Sub 3" />

    </PreferenceCategory>

</PreferenceScreen>
Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

0

I have done this before by using a custom preference theme and setting the android:textColor(title color) and android:textColorSecondary(summary color) in the custom theme. For example, black title and white summary:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
</style>

<!-- Custom Preference Theme -->
<style name="AppPreferenceTheme"
       parent="@style/PreferenceThemeOverlay.v14.Material">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

You can rename "AppPreferenceTheme" to whatever name you like.

Siu
  • 1,422
  • 1
  • 12
  • 17
  • This line is cousing issue (@style/PreferenceThemeOverlay.v14.Material) It is in red & complaining. What am I missing? –  Dec 20 '17 at 04:12
  • @Jonas Sorry, I was using the support library `preference-v14` and `PreferenceFragmentCompat` for better compatibility, so it is a bit different from yours. Without the support library, you may still change the text color by following [this answer](https://stackoverflow.com/a/14641302/3061187). You can find the title and summary `TextView` in `preference.xml`. Title color depends on `textAppearanceLarge` and summary color depends on `textColorSecondary `, so you can override them in your Activity theme to change color. – Siu Dec 20 '17 at 07:29