Switching application-wide theme programmatically?

Posted by Cheezmeister on Stack Overflow See other posts from Stack Overflow or by Cheezmeister
Published on 2011-01-11T23:13:51Z Indexed on 2011/01/13 0:54 UTC
Read the original article Hit count: 141

Filed under:
|
|

EDIT: Related question here:
Multi theme support in android app

I'm attempting to get a user-chosen theme and feel like I'm frustratingly close. Defining the theme in AndroidManifest.xml works as it should, but (as best I can tell) can't change based on app preferences:

<application 
    android:theme="@style/theme_sunshine" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">

Alternatively, setting it dynamically in each activity also works:

someChosenTheme = PreferenceManager.getDefaultSharedPreferences(this).getString("themePreference", "theme_twilight");
setTheme(someOtherChosenTheme);

But that seems messy, and I'd rather set the theme for the entire app in one place. My first thought was to grab the application context as soon as my main activity launches and do it there:

getApplicationContext().setTheme(R.style.theme_dummy);

As best I can tell[0], this ought to do the trick, but in fact it's not doing anything--the entire app has the default Android style. Is the above valid, and if so, might I be doing something else dumb?

I'm working in API level 3 if that matters. Prods in the right direction greatly appreciated!

[0] http://developer.android.com/reference/android/content/Context.html#setTheme%28int%29 http://developer.android.com/reference/android/content/Context.html#getApplicationContext%28%29

© Stack Overflow or respective owner

Related posts about java

Related posts about android