Overriding the layout used for a preference in a preferenceActivity/preferenceScreen using theming

4k views Asked by At

I have a preferenceActivity in my application and I have tried to set the preference style using the following theme:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="MyPreferenceTheme" parent="android:Theme.Translucent">
    <item name="android:preferenceStyle">@style/MyPreference</item>
    <item name="android:windowBackground">@color/transparent_black</item>
</style>

<style name="MyPreference" parent="@android:style/Preference">
    <item name="android:layout">@layout/preference</item>
</style>

<color name="transparent_black">#BB000000</color>

So I know that the theme is loading as the background is colored correctly. However my custom preferenceLayout (res/layout/preference.xml) is not getting applied to any of the preferences inside my preferenceActivity.

Is this the correct way to achieve theming of the preferences? or have I missed something? Thanks in advance :)

2

There are 2 answers

0
Paul On BEST ANSWER

I found that it's best not to use parent="android:style/Preference" as it doesn't seem to apply the style I'm trying to override it with. Style your layout that you're using (@layout/preference) and drop the inheritance from the android:style/Preference. It worked for me when I had to do the same.

so it should be:

<style name="MyPreference">
    <item name="android:layout">@layout/preference</item>
</style>

Good luck!

1
TomTasche On

That's a bug. See this issue.

You can "fix" it by assigning an ID for every PreferenceScreen. Then, you do this for every :

((PreferenceScreen) preferenceScreen).getDialog().getWindow().setBackgroundDrawable(drawable);

Good luck
Tom