title: Wrong button theme when switching Day/Night mode
body: <p>I have an app where I let the user switch between day and night mode, using the theme Theme.AppCompat.DayNight. However, randomly upon switching themes, the buttons have the wrong background color, and the same happens to the overflow button (it will be light on a light theme, or dark on a dark theme). I have written a minimal test app, and the problem is reproducible here too.</p>
  
  <p>Here's the sample app:</p>
  
  <p>styles.xml</p>
  
  <pre><code>&lt;resources&gt;
  
      &lt;!-- Base application theme. --&gt;
      &lt;style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"&gt;
          &lt;!-- Customize your theme here. --&gt;
          &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
          &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
          &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.Yellow"&gt;
          &lt;item name="colorAccent"&gt;@color/accent_yellow&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.LightBlue"&gt;
          &lt;item name="colorAccent"&gt;@color/accent_light_blue&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.Steel"&gt;
          &lt;item name="colorAccent"&gt;@color/accent_steel&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>colors.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;color name="colorPrimary"&gt;#3F51B5&lt;/color&gt;
      &lt;color name="colorPrimaryDark"&gt;#303F9F&lt;/color&gt;
      &lt;color name="colorAccent"&gt;#FF4081&lt;/color&gt;
  
      &lt;color name="accent_light_blue"&gt;#ff5677fc&lt;/color&gt;
      &lt;color name="accent_yellow"&gt;#ffffc107&lt;/color&gt;
      &lt;color name="accent_steel"&gt;#ff607d8b&lt;/color&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>arrays.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;array name="accent_colors"&gt;
          &lt;item&gt;@color/accent_light_blue&lt;/item&gt;
          &lt;item&gt;@color/accent_yellow&lt;/item&gt;
          &lt;item&gt;@color/accent_steel&lt;/item&gt;
      &lt;/array&gt;
  
      &lt;array name="accent_themes"&gt;
          &lt;item&gt;@style/AppTheme.LightBlue&lt;/item&gt;
          &lt;item&gt;@style/AppTheme.Yellow&lt;/item&gt;
          &lt;item&gt;@style/AppTheme.Steel&lt;/item&gt;
      &lt;/array&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout 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"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.quicosoft.daynighttest.MainActivity"
      tools:layout_editor_absoluteX="0dp"
      tools:layout_editor_absoluteY="24dp"&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Switch theme"
          tools:layout_editor_absoluteX="143dp"
          tools:layout_editor_absoluteY="262dp"
          app:layout_constraintLeft_toLeftOf="@+id/activity_main"
          tools:layout_constraintLeft_creator="0"
          app:layout_constraintTop_toTopOf="@+id/activity_main"
          tools:layout_constraintTop_creator="0"
          app:layout_constraintRight_toRightOf="@+id/activity_main"
          tools:layout_constraintRight_creator="0"
          app:layout_constraintBottom_toBottomOf="@+id/activity_main"
          tools:layout_constraintBottom_creator="0"
          app:layout_constraintVertical_bias="0.43" /&gt;
  
      &lt;CheckBox
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Checkbox"
          android:checked="true"
          tools:layout_editor_absoluteX="159dp"
          tools:layout_editor_absoluteY="214dp"
          app:layout_constraintLeft_toLeftOf="@+id/button"
          tools:layout_constraintLeft_creator="0"
          app:layout_constraintRight_toRightOf="@+id/button"
          tools:layout_constraintRight_creator="0"
          app:layout_constraintBottom_toTopOf="@+id/button"
          android:layout_marginBottom="16dp"
          tools:layout_constraintBottom_creator="0" /&gt;
  
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
          final boolean night = prefs.getBoolean("mode", false);
          AppCompatDelegate.setDefaultNightMode(night ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
  
          TypedArray ar = getResources().obtainTypedArray(R.array.accent_themes);
          int len = ar.length();
          int[] themes = new int[len];
          for (int i = 0; i &lt; len; themes[i] = ar.getResourceId(i++, 0)) ;
          ar.recycle();
          int colorIndex = new Random().nextInt(len);
          setTheme(themes[colorIndex]);
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  prefs.edit().putBoolean("mode", !night).apply();
                  finish();
                  startActivity(getIntent());
              }
          });
      }
  }
  </code></pre>
  
  <p>This is the button when the theme switches correctly:</p>
  
  <p><a href="https://i.stack.imgur.com/nNqGU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nNqGU.png" alt="This is the button when the theme switches correctly"></a></p>
  
  <p>This is the button when the theme switches incorrectly:</p>
  
  <p><a href="https://i.stack.imgur.com/UsN10.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UsN10.png" alt="This is the button when the theme switches incorrectly"></a></p>
  
  <p>For reference, here's a correct night theme (the problem happens both on the day and night modes, the day theme would have a button with a dark grey background instead of light grey, and the night theme would have a light grey background instead of dark grey - it's like the button retains the background from the previous theme, but the text inside switches to the correct theme):</p>
  
  <p><a href="https://i.stack.imgur.com/gmdBH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gmdBH.png" alt="Correct night theme"></a></p>
  
  <p>Any ideas why the button has the wrong background on occasion?</p>