I'm adding a Dark Mode to my app and I've created a checkbox in the 3-dot menu (toolbar).
I want to make the app change the theme to Dark when the checkbox is checked, and revert it back to the Main theme when unchecked.
Here is my current code for onClick of Dark Mode Checkbox button:
if (id == R.id.dark_mode) {
switch (item.getItemId()) {
case R.id.dark_mode:
if (item.isChecked()) {
// If item already checked then unchecked it
item.setChecked(false);
} else {
// If item is unchecked then checked it
item.setChecked(true);
}
default:
return super.onOptionsItemSelected(item);
}
}
How can I do this using java?
Also I would like to let the app remember whether the user lastly selected dark mode or not. (This is actually required because after the activity is restarted, the app would go back to it's old state.)