I was able to customize the preference's dialog theme without the support package. Since I switched to the support package (my preferences fragment now extends PreferenceFragmentCompat
instead of PreferencesFragment
) I'm not being able to change the colors.
So far I added this to my theme:
<item name="preferenceTheme">
@style/MyPreferenceTheme
</item>
<item name="android:dialogTheme">@style/DialogStyle</item>
<item name="android:alertDialogTheme">@style/DialogStyle</item>
<item name="android:dialogPreferenceStyle">@style/DialogStyle</item>
// ...
<style name="MyPreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<!--<style name="MyPreferenceTheme">-->
<item name="colorAccent">@android:color/white</item>
<item name="android:background">@color/bg0</item>
<item name="android:textColor">@color/fg0</item>
<item name="android:windowBackground">@color/bg0</item>
<item name="android:windowTitleStyle">@style/WindowTitleStyle</item>
<item name="android:keyTextColor">@color/fg0</item>
<item name="android:textColorAlertDialogListItem">@color/fg0</item>
<item name="android:textColorPrimary">@color/fg0</item>
<item name="android:textColorSecondary">@color/fg0</item>
<item name="android:textColorTertiary">@color/fg0</item>
<item name="colorControlNormal">@color/fg0</item>
<item name="colorControlActivated">@color/fg0</item>
<item name="colorControlHighlight">@color/fg0</item>
</style>
<style name="DialogStyle" parent="Theme.AppCompat.Dialog">
<item name="colorAccent">@android:color/white</item>
<item name="android:background">@color/bg0</item>
<item name="android:textColor">@color/fg0</item>
<item name="android:windowTitleStyle">@style/WindowTitleStyle</item>
<item name="android:windowBackground">@color/bg0</item>
<item name="android:keyTextColor">@color/fg0</item>
<item name="android:textColorAlertDialogListItem">@color/fg0</item>
<item name="android:textColorPrimary">@color/fg0</item>
<item name="android:textColorSecondary">@color/fg0</item>
<item name="android:textColorTertiary">@color/fg0</item>
<item name="colorControlNormal">@color/fg0</item>
<item name="colorControlActivated">@color/fg0</item>
<item name="colorControlHighlight">@color/fg0</item>
</style>
<style name="WindowTitleStyle" parent="TextAppearance.AppCompat.Title">
<item name="android:textColor">@color/fg0</item>
</style>
All I want is a black background and white text / widgets. fg0 is white and bg0 is black. But the dialog is showing a white bg and black text. The title is invisible (I assume it's white, which is correct).
In an act of despair I also tried to just use a custom layout for the dialog, with setDialogLayoutResource
- I wanted to copy the resource Android uses for the dialog and just change the colors. But I have no idea where the Android resource is. In the source code of the preferences there's no explicit reference to the layout files, other than that I found https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/res/layout/ and https://android.googlesource.com/platform/frameworks/support/+/master/v7/preference/res/layout/ but how do I know which is it?
How do I adjust the theme? This simple task can't be so complicated!
Also please explain what exactly the path is from "I want to set the background color of the preferences dialog to black" to the exact attributes I have to set in the XML. Where do I find: 1. Which dialog is this, 2. What attributes does it have 3. What are the effect of setting these attributes. (Preferably without having to dig in the source code).
Docs tell to look at the source, e.g. in R.attr but except searching for names containing "dialog" or "preferences" I don't know know what to do with this. None of the attributes I tried so far helped.
Related: Does nobody use (or restyle) PreferenceFragmentCompat
? Having difficulties finding informations about this and nobody seems to know either. What do you use instead? (these questions are only for the comments).
I found the problem:
I was using
<item name="android:alertDialogTheme">@style/DialogStyle</item>
and it should have been
<item name="alertDialogTheme">@style/DialogStyle</item>
Very irritating, in particular that Android Studio doesn't catch these errors and even autocompletes android:alertDialogTheme
.