1
votes

In my App (API 21) the user can switch between Dark and Light theme. The problem is when theme change activity background doesn't follow the new theme! I've tried :

  • different simulators and devices,same problem.
  • change androidmanifest.xml to theme
    "@android:style/Theme.Translucent.NoTitleBar" : not work with
    ActionBarActivity.
  • create a fake Translucent theme inherited from AppCompat : same
    problem.
  • force view one by one to apply new theme : activity doesn't like.

I made a sample App to simulate the problem some pictures will explain what I mean

This is DarkTheme default when setup in androidmanifest :

This is LightTheme default when setup in androidmanifest :

This is What I get when user switch from DarkTheme to Light Theme

This is What I get when user switch from LightTheme to DarkTheme

Here is Code Files :

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examples.testtheme" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/LightTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main_Activity.xml ( layout File)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView android:text="Sample TextView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large TextView Sample"
        android:textAppearance="?android:textAppearanceLarge"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        style="?android:starStyle"
        android:text="Like it? "/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="Try Change Theme..."/>


</LinearLayout>

MainActivity.class (java class)

public class MainActivity extends ActionBarActivity {
    static int themeid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("oncreate","static value is " + themeid);
        super.setTheme(themeid);
        this.setTheme(themeid);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_theme_dark) {
            super.setTheme(R.style.DarkTheme);
            themeid = R.style.DarkTheme;
            Log.w("optionmenuselect","Dark ID= " + R.style.DarkTheme);
            this.recreate();
        }
        if (id == R.id.action_theme_light){
            super.setTheme(R.style.LightTheme);
            themeid = R.style.LightTheme;
            Log.w("optionmenuselect","Light ID= " + R.style.LightTheme);
            this.recreate();
        }

        return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/action_theme_light" android:title="Light Theme"/>
    <item android:id="@+id/action_theme_dark" android:title="Dark Theme"/>
</menu>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="LightTheme" parent="Theme.AppCompat.Light" >
        <!-- Customize your theme here. -->
    </style>
    <style name="DarkTheme" parent="Theme.AppCompat">

    </style>

</resources>
1

1 Answers

3
votes

You simply need to move setTheme(themeid) before super.onCreate(savedInstanceState).