Programmatically?

I am trying to get a user-selected theme and I feel upset. The theme definition in AndroidManifest.xml works as it should, but (as far as I can tell) it cannot change depending on the settings of the application:

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

Alternatively, setting dynamically in each action also works:

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

But that seems messy, and I would rather install the theme for the entire application in one place. My first thought was to capture the context of the application as soon as my main activity starts and do this:

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

As far as I can tell, this one should do the trick, but in fact it does nothing - the whole application has a default value of Android Style. Is this higher, and if so, can I do something else dumb?

I work at API level 3 if that matters. Virtues in the right direction are greatly appreciated!

Related question.

+36
android themes android-theme
Jan 11 '11 at 23:13
source share
2 answers

Create basic activity for your application and override onCreate to set the theme. Print all your other actions from this base.

Also check out this tutorial: http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

+30
Jan 12 '11 at 19:37
source share

Perhaps this may be a good solution, but I could not find it, so I created a small static helper method in a helper class that executes the Activity and basically executes the two lines you wrote. Of course, this is not ideal, but just adding one short line of code for each onCreate() method in my application was acceptable.

+5
Jan 11 '11 at 23:17
source share



All Articles