Search code examples
javaandroidcheckboxtoolbarandroid-toolbar

Changing app theme with checkbox (with java)


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.)


Solution

  • To change the theme of your app programatically, you can use setTheme(...) in your onCreate() method right before super.onCreate() (code from this question):

    public void onCreate(Bundle savedInstanceState) {
        setTheme(android.R.style.Theme); // THIS IS WHERE THE THEME IS SET -- must go before super.onCreate()
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
    

    However, this won't change the theme once the app is already going. To do that, you will need to either restart the app or just change the backgrounds of your activities without actually changing the theme.

    Restarting the app:

    Code comes from this question, answer by Marc:

    Intent i = getBaseContext().getPackageManager()
                 .getLaunchIntentForPackage( getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    

    Changing the backgrounds:

    If you need to change the background while the app is still running, you can set the background color of a layout like so (here, it's being set to red):

    layout.setBackgroundColor(Color.RED);
    

    To save the user's choice after the app closes:

    The best way to do this is to use the SharedPreferences API. If you want to save the background choice as a String-to-boolean key-value pair, for example (where the key would be the String "background_is_dark" and the value would be either true or false) you could write this:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putBoolean("background_is_dark"), true); // here is where you would enter true or false
    editor.commit();
    

    To later access that boolean value (like in onCreate() when you need to decide which background to set) you would use this code:

    Context context = getActivity();
    SharedPreferences isDark = context.getSharedPreferences(
            "background_is_dark", Context.MODE_PRIVATE);
    

    See Android's documentation for more information on the SharedPreferences API.