0

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

Arda Çebi
  • 1,705
  • 4
  • 15
  • 27
  • What is the issue you are facing? – Yupi Jan 31 '18 at 23:16
  • I spent a good week once trying to figure out how to do this. Short answer, you can't change the actual theme dynamically without restarting the app. You can change the background color of activities dynamically, but button colors and other theme properties like that are much harder to do from what I know of it. – Keara Jan 31 '18 at 23:18
  • @Keara so when I click the checkbox, can I make the activity (or app) restart? Will that work? – Arda Çebi Jan 31 '18 at 23:20
  • I think so, although I can't remember exactly how to restart the app programatically. Try this S.O. question for starters:https://stackoverflow.com/questions/15564614/how-to-restart-an-android-application-programmatically – Keara Jan 31 '18 at 23:23
  • Thank you @Keara but before restarting the app, how can I change the theme programmatically? – Arda Çebi Jan 31 '18 at 23:25
  • I'll write an answer - stay tuned! – Keara Jan 31 '18 at 23:28
  • @Keara waiting for it :) – Arda Çebi Jan 31 '18 at 23:31

1 Answers1

1

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.

Keara
  • 589
  • 1
  • 6
  • 17
  • Again, thank you so much @Keara. I was looking for a solution to this for weeks! – Arda Çebi Jan 31 '18 at 23:46
  • I know that feeling!! I wish there was a better way to do this - it seems like a common thing people want to do. :) Let us know if any other issues come up. – Keara Jan 31 '18 at 23:47
  • Hello Again, I would like to ask something. I've tried the code you've provided for me to change my app theme to something dark when the user checks the checkbox but, it doesn't work actually. I need to let the app remember that the user selected dark mode, after the app restart app loads the standard theme and the dark mode checkbox returns to unchecked. Can you edit your answer and provide another code snippet also for this? Thank you. – Arda Çebi Feb 01 '18 at 19:42
  • I don't have enough information on how to use the local database on Android using Java :( – Arda Çebi Feb 01 '18 at 19:43
  • Hi Arda, you could edit your question to include that requirement, but I would actually ask a whole new question about that. It's best to put one question per post on Stack Overflow. – Keara Feb 01 '18 at 20:34