I'm create app with 2 activity and want set theme onclick button in first and second activity.
method setTheme() work only on first activity
if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.OLEDTheme);
} else {
setTheme(R.style.AppTheme);
}
Switch oledModeSwitch = (Switch) findViewById(R.id.OLEDSwitch);
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
oledModeSwitch.setChecked(true);
}
oledModeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean Check) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
finish();
if (Build.VERSION.SDK_INT >= 21) {
overridePendingTransition(R.anim.from_alpha, R.anim.to_alpha);
}
startActivity(intent);
if (Check) {
Toast.makeText(getApplicationContext(), "Тема успешно активирована", Toast.LENGTH_SHORT).show();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Toast.makeText(getApplicationContext(), "Тема успешно дезактивирована", Toast.LENGTH_SHORT).show();
}
}
});
Theme set only first activity, but I expect set on first and second activity
When you call setTheme()
it only changes the theme for the Activity
that made the call. If you want to change the theme for your entire application, then you should record the theme setting in SharedPreferences
and then in each Activity
you should read the current theme from SharedPreferences
and call setTheme()
in that Activity
.