I'm trying to make a navigation drawer switch which switches between a normal theme and a dark theme of an app, however I can't get the switch to work.
I already have a working switch on mainactivity but I can't get it to work in the navigation drawer.
This is the code to switch between light/dark mode.
@Override
protected void onCreate(Bundle savedInstanceState) {
if(AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.HROTheme);
}
else setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modeswitch=(Switch)findViewById(R.id.switch2);
if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
modeswitch.setChecked(true);
}
modeswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
}
}
});
You can try to add an item in values/attrs like this: <attr name="bottomback" format="color" />
Then define "bottomback" in styles for your both dark and light styles like this:
<item name="bottomback">#000</item>
That must be different in both styles. now you can set the backgroundTint of drawer as this: android:backgroundTint="?attr/bottomback"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.