Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.NoTitleBar" />

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.

EDIT (PROBLEM FOUND): So after trying it turn out that on Android 2.2 Manifest file set Background and rest, and programmatically changing theme affect only text color. any idea why that happens?

ANSWER (as I don't have 10 reputation): On android <3.0 it matters if

super.onCreate(savedInstanceState);

Is before setting up theme or not.

So it should be:

public void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_NoBar);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista);
}

Sorry guys for making problem out of nothing but as it was working on >3.0 I was confused. Spend half of day on it but working. :)

like image 626
Michał Tajchert Avatar asked Oct 23 '22 07:10

Michał Tajchert


1 Answers

There's a mistake in your styles.xml: for Theme.NoBarBlack, you set the parent to @android:style/Theme.NoActionBar, but it doesn't exist.

I think you mean @android:style:Theme.NoTitleBar.

like image 200
Kyriog Avatar answered Nov 04 '22 19:11

Kyriog