1

Could someone tell me how i can switch the the theme from holo to holo light in my application on runtime ? I would like to have two buttons in settings to choose light or black theme. How can it be set applicationwide and not only for the activity ?

I already tried a few things with setTheme() but i wasn't able to change the theme when i click a button.

This is my Settings activity where i would like to set the theme:

public class SettingsActivity extends Activity  {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {


setTheme(android.R.style.Theme_Holo_Light);

super.onCreate(savedInstanceState);

setContentView(R.layout.settings);

}

well this works and my Theme is set but as i am saying i would like to change it systemwide by pressing a button.

Thanks !

zFr3eak
  • 235
  • 1
  • 6
  • 21
  • possible duplicate of [Switching application-wide theme programmatically?](http://stackoverflow.com/questions/4663752/switching-application-wide-theme-programmatically) – Bryan Herbst Oct 16 '13 at 19:46
  • If one of the answers helped you, you can accept it. Otherwise you can post an answer with what you did to fix it. You don't need to post thanks as an answer. – dcaswell Oct 17 '13 at 18:01

2 Answers2

3

As you can see theme is setting on the onCreate() and before setContentView(). So you should call the oncreate() method again when you want to change the theme. But the onCreate() function will be called only once in the life cycle of an Activity.

There is a simple way to do this. I am not sure that it is the best way.

Suppose you want to apply new theme to Activity 1 on a button click.

inside the onClick event

  1. Save the theme to be applied such that it should be retained even after the application restart (preferences or static volatile variables can be used).
  2. Finish the current activity (Activity 1) and call a new activity (Activity 2).

Now in Activity 2

  1. Call Activity 1 and finish current activity (Activity 2).

In Activity 1

  1. Apply the saved theme inside onCreate.

Hope it is not confusing.. :)

Anu
  • 1,884
  • 14
  • 30
2

You cannot change the theme of other applications (thank goodness).

The only way to somewhat accomplish this would be to create your own build of the operating system with your own theme as the device default theme. However, applications that do not use the device default theme (i.e. they explicitly set the theme to Holo, Holo light, etc) will not get the device default theme.

Edit- To accomplish this application-wide using a base Activity, create an Activity that looks like this:

public class BaseActivity extends Activity {
    private static final int DEFAULT_THEME_ID = R.id.my_default_theme;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        int themeId = PreferenceManager.getDefaultSharedPreferences(context)
                .getInt("themeId", DEFAULT_THEME_ID);
        setTheme(themeId);

        super.onCreate(savedInstanceState);
    }
}

Then all of your Activities should extend this BaseActivity. When you want to change the theme, be sure to save it to your SharedPreferences.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • i guess you missunderstood me. i don't want to change the theme of all applications i meant that i have my application (a calculator) with a default theme (Holo.light) and i want to change the theme from holo.light to holo with a on/off toogle only in my calculator app. – zFr3eak Oct 16 '13 at 19:28
  • Ah, in that case see [Switching application-wide theme programmatically?](http://stackoverflow.com/q/4663752/1253844). – Bryan Herbst Oct 16 '13 at 19:45
  • i saw that already, but i wasn't able to reproduce it. any chance for a short example ? – zFr3eak Oct 16 '13 at 20:09