How to change theme (or style) for PreferenceActivity

2.6k Views Asked by At

I am using holoeverywhere. I have created my own theme for my app and I can apply this theme to my activities with success. My theme changes drawables for CheckBox, EditText, DatePicker and so on. The problem is that this theme does npot work for PreferenceActivity. What I find puzzling is if I set the theme to Holo.Theme or Holo.Theme.Light it changes as it should. But if I change it to my theme which is derived from Holo.Theme.Light it does not. Yet, an all other activities it does work. I change activity theme via:

activity.setTheme(R.style.MyCustomTheme);

All this works for all other activities just not for PreferenceActivity. Are there some undocumented atributes which must be set to change preference activity theme?

2

There are 2 best solutions below

4
On

No, it's not undocumented: "I put custom preference styles in the activity theme, but nothing happened!".

For preference items/views using other themes (not the activity theme), you should remap the theme with the PreferenceInit.map method.

import org.holoeverywhere.app.Application;

public class MyApplication extends Application {
  static {
    PreferenceInit.map(R.style.CustomPreferenceTheme, R.style.CustomPreferenceTheme_Light);
  }
}
3
On

styles.xml

<style name="SettingsTheme" parent="android:Theme.Light">
   <!-- Override properties according to your need -->
   <item name="android:colorBackground">#ffffff</item>
   <item name="android:colorForeground">#aaaaaa</item>

   <item name="android:textColorPrimary">#ff0000</item>
   <item name="android:textColorSecondary">#0000ff</item>
   <item name="android:textColorTertiary">#00ff00</item>
   <item name="android:textColorPrimaryDisableOnly">#888888</item>
   <item name="android:textColorHint">#778899</item>
</style>

Apply your newly defined style to Preference Activity in AndroidManifest.xml using android:theme

<activity android:name=".SettingsThemeActivity"
      android:label="@string/app_name"
      android:theme="@style/SettingsTheme">

Edit: Try to set the the theme before

setTheme(R.style.SettingsTheme);

setContentView(R.layout.main);