I\'m trying to change the theme for the PreferenceActivity in my app and I just can\'t get it to work.
This is the xml:
I personally use this approach: for API below 11 I use(values directory, themes.xml file):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Light.NoTitleBar"></style>
</resources>
for higher (for example values-v14 directory, themes.xml file):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
</resources>
and manifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Android will choose a theme based on device's API automatically, and no need to specify any themes in activities (manifest) or grammatically in code...
This is gonna sound stupid, but using setTheme()
will work on all your fragments, but not the main preference activity. Set this in the manifest file, and it will suddenly start working.
I set the theme dynamically based on user preferences, calling setTheme()
before the onCreate
from theme manager object (it also doles out various colors and drawables
that can be changed by the theme that I don't want to set in styles ... mainly because its too confusing to figure out what is what).
No idea why setting it in the manifest makes setTheme()
work. Just some glitch specific to the preference activity I guess.
If you want to change the background you could use
public class FractalPreferenceActivity extends PreferenceActivity {
.......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawableResource(R.drawable.gradient);
getListView().setBackgroundColor(Color.TRANSPARENT);
getListView().setCacheColorHint(Color.TRANSPARENT);
.......
}
}
Have you tried applying the theme on the activity tag in the manifest? This is how I have done it before -
<activity
android:label="@string/app_name"
android:name="com.example.MyPreferenceActivity"
android:theme="@android:style/Theme.Black"
android:exported="true"
android:icon="@drawable/ic_launcher"></activity>
EDIT:
The other option you could try is to override onApplyThemeResource(Resources.Theme theme, int resid, boolean first)
. Looking at the android source code the setTheme will internally call method.
/**
* Called by {@link #setTheme} and {@link #getTheme} to apply a theme
* resource to the current Theme object. Can override to change the
* default (simple) behavior. This method will not be called in multiple
* threads simultaneously.
*
* @param theme The Theme object being modified.
* @param resid The theme style resource being applied to <var>theme</var>.
* @param first Set to true if this is the first time a style is being
* applied to <var>theme</var>.
*/
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
theme.applyStyle(resid, true);
}
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.