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>