androidkotlinandroid-fragmentspreferencescreen

How to change text color and background color of PreferenceScreen in Android


How to change text and background color in a PreferenceScreen inflated by PreferenceFragmentCompat?

Tried How to change the text color of PreferenceCategory/PreferenceScreen but the solution works in a Preference Activity and not in PreferenceFragmentCompat.

Also tried using the layout tag in the preference screen as per How to change text color of preference category in Android? but doesn't save preference.

class FragmentSettings: PreferenceFragmentCompat() {

    private lateinit var viewModel: SharedViewModel

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.root_preferences)

        val application = requireNotNull(this.activity).application
        val dataBase = DataBase.getInstance(application)
        val repo = Repo(dataBase!!)

        viewModel = ViewModelProvider(this,
            SharedViewModelFactory(
                dataBase
            )
        ).get(SharedViewModel::class.java)

        (activity as MainActivity).supportActionBar?.title = "Settings"
}
            

This is the code in my settingsPreference.xml.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorExtra"
    android:backgroundTint="@color/colorExtra">

    <PreferenceCategory android:layout="@layout/pref_title" app:title="@string/location_header">

        <SwitchPreferenceCompat
            android:background="@color/colorExtra"
            android:backgroundTint="@color/colorExtra"
            app:defaultValue="true"
            android:layout= "@layout/switch_pref_item"
            app:disableDependentsState="true"
            app:key="USE_DEVICE_LOCATION"
            app:summary="Allow the app to get your location"
            app:title="@string/your_location_title" />

        <EditTextPreference
            android:background="@color/colorExtra"
            android:backgroundTint="@color/colorExtra"
            app:dependency="USE_DEVICE_LOCATION"
            app:key="setLocation"
            android:layout="@layout/set_location_pref"
            app:title="@string/set_location_title"
            app:useSimpleSummaryProvider="true" />


    </PreferenceCategory>

</PreferenceScreen>

Solution

  • This should be done via styles, and follows the same procedure used to style any layout. It is not done via the preferences xml.

    Make an xml style that is a child of Theme.AppCompat. Then you can override the colors for android:windowBackground and android:textColor. You can apply this style to the activity in the manifest.

    For example, in styles.xml:

    <color name="preferencesTextColor">$ff0000</color>
    <color name="preferencesBackgroundColor">#202020</color>
    
    <style name="MyPreferencesTheme" parent="Theme.AppCompat">
        <item name="android:textColor">@color/preferencesTextColor</item>
        <item name="android:windowBackground">@color/preferencesBackgroundColor</item>
    </style>
    

    Then in the manifest, apply the theme to the activity:

    <activity android:name="MyPreferencesActivity"
        android:theme="@style/MyPreferencesTheme"
        android:label="@string/myPreferenceActivityLabel" />