PreferenceActivity and theme not applying
Somebody just posted a workaround at http://code.google.com/p/android/issues/detail?id=4611
In a nutshell, top level preference screens seem to recognize the theme but nested ones not. So the workaround recommends creating top level PreferenceActivity for nested PreferenceScreen and then invoking this new activity via intent:
<PreferenceScreen android:key="key1"
android:title="1 Item"
android:summary="">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="com.example"
android:targetClass="com.example.PreferenceActivity2"/>
</PreferenceScreen>
I didn't have to apply the theme to anything but the application itself.
You can also use this technique to override the styles of the inner preference screens :
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference != null) {
if (preference instanceof PreferenceScreen) {
if (((PreferenceScreen) preference).getDialog() != null) {
((PreferenceScreen) preference)
.getDialog()
.getWindow()
.getDecorView()
.setBackgroundDrawable(
this
.getWindow()
.getDecorView()
.getBackground()
.getConstantState()
.newDrawable()
);
}
}
}
return false;
}
This code applies the style of the main preference screen to the clicked preference screen.
At last i found out how to change theme of "PreferenceActivity" programmatically(via java code)
To change theme just do like this:
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
}
Always call setTheme(R.style.yourtheme);
method before super.onCreate(savedInstanceState);
method. By doing this it will produce result as shown below.
That's all.
If yo call setTheme(R.style.yourtheme);
method after super.onCreate(savedInstanceState);
method it will produce result as shown below.
Note: Themes are not recognize by nested PreferenceScreen. To apply theme to that nested PreferenceScreen you have to make an another PreferenceActivity for that nested PreferenceScreen and call setTheme(R.style.yourtheme);
method there.