title: How to extend dark theme to every activity & fragment in Android?
body: <p>I'm trying to add dark mode to my android application but I can't apply it to all activities and fragments. I specify that the application is started with the light mode and I would like to make the user change through the switch which mode to use. Thanks everyone in advance.</p>
  <p>styles.xml</p>
  <pre><code>&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
      &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
      &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
      &lt;item name=&quot;colorAccent&quot;&gt;@color/black&lt;/item&gt;
  
      &lt;item name=&quot;android:textColor&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;android:colorButtonNormal&quot;&gt;@color/white&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name=&quot;DarkTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
      &lt;item name=&quot;colorPrimary&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;colorAccent&quot;&gt;@color/black&lt;/item&gt;
  
      &lt;item name=&quot;android:background&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;cardBackgroundColor&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;android:textColor&quot;&gt;@color/white&lt;/item&gt;
      &lt;item name=&quot;android:colorButtonNormal&quot;&gt;@color/black&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  <p>activity_settings.xml</p>
  <pre><code>&lt;Switch
      android:id=&quot;@+id/switch_theme&quot;
      android:layout_width=&quot;114dp&quot;
      android:layout_height=&quot;46dp&quot;
      android:layout_marginTop=&quot;48dp&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintHorizontal_bias=&quot;0.835&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/info_darkMode&quot; /&gt;
  </code></pre>
  <p>public class SettingsActivity extends AppCompatActivity {</p>
  <pre><code>public static final String IS_DARK = &quot;IS_DARK&quot;;
  
  @Override
  protected void attachBaseContext(Context baseContext) {
      super.attachBaseContext(baseContext);
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(baseContext);
      boolean isDark = prefs.getBoolean(IS_DARK, false);
  
      if (isDark)
          AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
      else
          AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
  }
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_settings);
  
      TextView textView = findViewById(R.id.info_darkMode);
      Switch switchBtn = findViewById(R.id.switch_theme);
      final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  
      
      if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
          switchBtn.setChecked(true);
      }
  
      switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
              if(isChecked) {
                  AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                  prefs.edit().putBoolean(IS_DARK, true).apply();
              }
              else {
                  AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                  prefs.edit().putBoolean(IS_DARK, false).apply();
              }
          }
      });
  }
  </code></pre>
  <p>}</p>