2

how can we dynamically switch on or off from one Theme to another Theme for whole application...i have try to find everywhere but cant get success.....so,please provide any source code...

shripal
  • 1,222
  • 4
  • 19
  • 40
  • You have to set the theme from the XML file which suggests that it is done before the onCreate. Therefore i would guess that you might have to make the change to the settings and then restart the activity. – Robert Massaioli Mar 02 '11 at 05:15
  • how can we make changes into setting can u please suggest me source code??? please – shripal Mar 02 '11 at 06:29
  • He just told you. It's unlikely he's going to `suggest you` any source code; that's the part that you, as the developer, has to do. –  May 23 '11 at 13:03

3 Answers3

2

setTheme() does work, you just have to be sure to set the theme before any of the system framework starts building views. I have applications that can switch themes on the fly but you must call setTheme() first in your onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {

    // The only way the custom themes apply properly is to assign them before any view
    // resources are created.
    SettingsActivity.setThemeFromPreferences(this);

    super.onCreate(savedInstanceState);
}

Where the setThemeFromPreferences method looks up the setting and calls setTheme with the appropriate style. Also take a look at Ben's answer to this post about restarting an activity that you may want to use to apply your theme changes on the fly after they are changed by the user.

Community
  • 1
  • 1
Jerry Brady
  • 3,050
  • 24
  • 30
  • The most important thing when switching themes is to use **setTheme()** method before **onCreate** process: **super.onCreate()** or **setContentView()**. – Michal May 22 '13 at 21:28
1

You can try with setTheme, but you might run into monsters.

Felix
  • 88,392
  • 43
  • 149
  • 167
0

There is an open Google Android issue for this: it appears to be an open bug that you cannot programmatically change the theme with setTheme(). http://code.google.com/p/android/issues/detail?id=4394

Lucy
  • 684
  • 1
  • 8
  • 15