I want dynamically change the theme of my app with buttons, so I implemented this:
sharedPreferences = getSharedPreferences("VALUES",MODE_PRIVATE);
int theme = sharedPreferences.getInt("THEME",2);
switch (theme){
case 1: setTheme(R.style.AppTheme);
break;
case 2: setTheme(R.style.AppTheme_AppBarOverlay);
break;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutotial);
And this is the code of the buttons:
tb1 =(Button) findViewById(R.id.button2);
tb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",1).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
}
});
tb2 =(Button) findViewById(R.id.button3);
tb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",2).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
Intent intent1 = new Intent(tutotial.this, MainActivity.class);
startActivity(intent1);
}
});
The problem is that code just changes the theme of the activity associated and it does not make the change theme in all the app.