Theme change doesn't work on <4.0 as it should

Posted by user1717276 on Stack Overflow See other posts from Stack Overflow or by user1717276
Published on 2012-10-06T21:35:50Z Indexed on 2012/10/06 21:37 UTC
Read the original article Hit count: 291

I have some difficulties with setting up a "theme switcher" programmatically. I would like to switch themes from app (between White (Theme.Light.NoTitleBar) and Dark (Theme.Black.NoTitleBar)) and what I do is: I set a SharedPreference:

final String PREFS_NAME = "MyPrefsFile";
    final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    final SharedPreferences.Editor editor = settings.edit();

and than I have a two buttons to switch themes (second one is almost identical)

Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
    ThemeWhite.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            editor.putBoolean("Theme", false);
              editor.commit();
              System.exit(2);
        }
    });

and in begging of each activity I check SharedPreference

boolean theme = settings.getBoolean("Theme", false);
    if(theme){
        this.setTheme(R.style.Theme_NoBarBlack);
    } else{
        this.setTheme(R.style.Theme_NoBar);
    }

    setContentView(R.layout.aplikacja);

I define themes in file styles.xml in folder values:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.NoActionBar" />

in values-v11:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.Holo.NoActionBar" />

in values-v14:

<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.DeviceDefault.NoActionBar" />

manifest file:

<application
    android:theme="@style/Theme.NoBar" >

Everything is working excellent on android >4.0 but when I use 2.2 it doesn't change theme - just font is getting white as it should be but there is no dark background.

I tried checking if it at least works and changed Theme.NoBarBlack in values (for android <3.0) and its value the same as Theme.NoBar and then when I pressed button font wasn't changed -as it should do.

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout