title: How to Change android app theme in runtime?
body: <p>I'm trying to change my app theme during runtime</p>
  <p>I've my ChangeTheme class and Utility(SharedPreferences) class</p>
  <blockquote>
  <p>ChangeTheme.java</p>
  </blockquote>
  <pre><code>public class ChangeTheme extends AppCompatActivity {
  
      private final static int THEME_LIGHT = 1;
      private final static int THEME_DARK = 2;
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.change_theme_activity);
          updateTheme();
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.myoptions,menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(@NonNull MenuItem item) {
          int id = item.getItemId();
  
          switch (id){
              case R.id.theme:
                  Intent toChangeTheme = new Intent(ChangeTheme.this,ChangeTheme.class);
                  startActivity(toChangeTheme);
          }
  
          return true;
      }
  
      public void onRadioButtonClicked(View view) {
          // Is the button now checked?
          boolean checked = ((RadioButton) view).isChecked();
  
          // Check which radio button was clicked
          switch(view.getId()) {
              case R.id.lightTheme:
                  if (checked){
                      setTheme(R.style.AppTheme);
                  }
  
                      break;
              case R.id.darkTheme:
                  if (checked){
                      setTheme(R.style.AppThemeDark);
                  }
  
                      break;
          }
      }
  
      public void updateTheme() {
          if (Utility.getTheme(getApplicationContext()) &lt;= THEME_LIGHT) {
              setTheme(R.style.AppTheme);
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                  getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                  getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
              }
          } else if (Utility.getTheme(getApplicationContext()) == THEME_DARK) {
              setTheme(R.style.AppThemeDark);
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                  getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                  getWindow().setStatusBarColor(getResources().getColor(R.color.colorSecondary));
              }
          }
      }
  
  }
  </code></pre>
  <blockquote>
  <p>change_theme_activity.xml</p>
  </blockquote>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:paddingTop=&quot;12dp&quot;
      android:paddingLeft=&quot;8dp&quot;
      android:paddingRight=&quot;8dp&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textTheme&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Change Theme To:&quot;
          android:layout_marginTop=&quot;125dp&quot;&gt;
      &lt;/TextView&gt;
  
      &lt;RadioGroup
          xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;vertical&quot;
          android:layout_marginTop=&quot;20dp&quot;&gt;
          &lt;RadioButton
              android:id=&quot;@+id/lightTheme&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;Light Theme&quot;
              android:onClick=&quot;onRadioButtonClicked&quot;/&gt;
          &lt;RadioButton
              android:id=&quot;@+id/darkTheme&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;Dark Theme&quot;
              android:onClick=&quot;onRadioButtonClicked&quot;/&gt;
      &lt;/RadioGroup&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <blockquote>
  <p>Utility.java</p>
  </blockquote>
  <pre><code>public class Utility {
      public static void setTheme(Context context, int theme) {
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
          prefs.edit().putInt(context.getString(R.string.prefs_theme_key), theme).apply();
      }
      public static int getTheme(Context context) {
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
          return prefs.getInt(context.getString(R.string.prefs_theme_key), -1);
      }
  }
  </code></pre>
  <blockquote>
  <p>styles.xml</p>
  </blockquote>
  <pre><code>&lt;resources&gt;
  
      &lt;!-- Base application theme. --&gt;
      &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
          &lt;!-- Customize your theme here. --&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/colorAccent&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name=&quot;AppThemeDark&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
          &lt;!-- Customize your theme here. --&gt;
          &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorSecondary&lt;/item&gt;
          &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorSecondaryDark&lt;/item&gt;
          &lt;item name=&quot;colorAccent&quot;&gt;@color/colorSecondaryAccent&lt;/item&gt;
      &lt;/style&gt;
  
  &lt;/resources&gt;
  </code></pre>
  <p>When i click in the buttons to change the theme nothing happens, why?
  Can you help me?
  I've already checked similar questions but nothing've helped</p>
  <h2>Update 2020/09/08</h2>
  <blockquote>
  <p>ChangeTheme.java</p>
  </blockquote>
  <pre><code>public class ChangeTheme extends AppCompatActivity {
  
      private final static int THEME_LIGHT = 1;
      private final static int THEME_DARK = 2;
      private RadioButton lightThemeButton;
      private RadioButton darkThemeButton;
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.change_theme_activity);
  
          lightThemeButton = (RadioButton) findViewById(R.id.lightTheme);
          darkThemeButton = (RadioButton) findViewById(R.id.darkTheme);
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.myoptions,menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(@NonNull MenuItem item) {
          int id = item.getItemId();
  
          switch (id){
              case R.id.theme:
                  Intent toChangeTheme = new Intent(ChangeTheme.this,ChangeTheme.class);
                  startActivity(toChangeTheme);
          }
  
          return true;
      }
  
      public void onRadioButtonClicked(View view) {
          // Is the button now checked?
          boolean checked = ((RadioButton) view).isChecked();
  
          // Check which radio button was clicked
          switch(view.getId()) {
              case R.id.lightTheme:
                  if (checked){
                      Utility.changeToTheme(this, Utility.THEME_LIGHT);
                  }
                  break;
              case R.id.darkTheme:
                  if (checked){
                      Utility.changeToTheme(this, Utility.THEME_DARK);
                  }
                  break;
          }
      }
  }
  </code></pre>
  <blockquote>
  <p>Utility.java</p>
  </blockquote>
  <pre><code>public class Utility {
  
      private static int sTheme;
      public final static int THEME_LIGHT = 0;
      public final static int THEME_DARK = 1;
  
      //Set the theme of the Activity, and restart it by creating a new Activity of the same type.
      public static void changeToTheme(Activity activity, int theme)
      {
          sTheme = theme;
          activity.finish();
          activity.startActivity(new Intent(activity, activity.getClass()));
      }
  
      //Set the theme of the activity, according to the configuration. */
      public static void onActivityCreateSetTheme(Activity activity)
      {
          switch (sTheme)
          {
              default:
              case THEME_LIGHT:
                  activity.setTheme(R.style.AppTheme);
                  break;
              case THEME_DARK:
                  activity.setTheme(R.style.DarkTheme);
                  break;
          }
      }
  }
  </code></pre>
  <blockquote>
  <p>styles.xml</p>
  </blockquote>
  
  <pre><code>&lt;color name=&quot;color_primary_grey&quot;&gt;#212121&lt;/color&gt;
  &lt;color name=&quot;color_primary_black&quot;&gt;#000000&lt;/color&gt;
  &lt;color name=&quot;color_accent_white&quot;&gt;#aeaeae&lt;/color&gt;
  
  &lt;!-- Base application theme. --&gt;
  &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
      &lt;!-- Customize your theme here. --&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/colorAccent&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name=&quot;DarkTheme&quot; parent=&quot;AppTheme&quot;&gt;
      &lt;item name=&quot;colorPrimary&quot;&gt;@color/color_primary_grey&lt;/item&gt;
      &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/color_primary_black&lt;/item&gt;
      &lt;item name=&quot;colorAccent&quot;&gt;@color/color_accent_white&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>What i've to do now so i can change the theme in runtime?</p>