0

I have a layout that changes background colours, tint lists and text colours for a dark/light mode function.

There are only two settings (dark/light) and the colours used within the themes are referenced at the top of my code like this:

    int orangeTheme = Color.rgb(253,135,0);
    int blueTheme = Color.rgb(0,0,254);
    int whiteTheme = Color.rgb(213,214,214);
    int blackTheme = Color.rgb(0,0,0);

set like this: (dark mode)

gridLayout.setBackgroundColor(blackTheme);

buttonA.setBackgroundTintList(ColorStateList.valueOf(blueTheme));
buttonA.setTextColor(blackTheme);

buttonB.setBackgroundTintList(ColorStateList.valueOf(orangeTheme));
buttonB.setTextColor(blackTheme);

seekBar.setProgressTintList(ColorStateList.valueOf(blueTheme));
seekBar.setThumbTintList(ColorStateList.valueOf(blueTheme));

and reverted like this: (light mode)

gridLayout.setBackgroundColor(orangeTheme);

buttonA.setBackgroundTintList(ColorStateList.valueOf(blackTheme));
buttonA.setTextColor(ColorStateList.valueOf(whiteTheme));

buttonB.setBackgroundTintList(ColorStateList.valueOf(blueTheme));
buttonB.setTextColor(ColorStateList.valueOf(orangeTheme));

seekBar.setProgressTintList(ColorStateList.valueOf(blackTheme));
seekBar.setThumbTintList(ColorStateList.valueOf(blackTheme));

Using shared preferences, how do I save and load these values?

public void saveData() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.apply();
        Toast.makeText(FavouriteActivity.this, "Data saved", Toast.LENGTH_SHORT).show();
    }

public void loadData() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);

        Toast.makeText(FavouriteActivity.this, "Data loaded", Toast.LENGTH_SHORT).show();
    }

public void updateViews() {

        Toast.makeText(FavouriteActivity.this, "Views updated" , Toast.LENGTH_SHORT).show();
    }

I have very little experience with Android & Java and all of the save state tutorials I've seen handle text view contents or the position of a switch and not much more than that. This is why I'm asking - I'm still unsure how to save or reference most things, but for now I'm mainly focused on the following:

  • constraint layout background colour
  • grid layout background colour
  • button background colour
  • button background tint
  • button text colour
  • button visibility

How would I reference and retrieve these aspects in a shared preferences save/load function?

Is there a better way to save and load these aspects?

Thanks for any and all help.

dewie
  • 99
  • 1
  • 8

1 Answers1

0

my answer is: you should not be doing this manually. Instead, create two resource files (styles files for these components) and then use AppCompatDelegate.setDefaultNightMode

Have a look at this:

setDefaultNightMode added in version 24.1.0

void setDefaultNightMode (int mode)

Sets the default night mode. This is used across all activities/dialogs but can be overridden locally via setLocalNightMode(int).

This method only takes effect for those situations where applyDayNight() works. Defaults to MODE_NIGHT_NO.

This only takes effect for components which are created after the call. Any components which are already open will not be updated.

which takes one of the following values:

MODE_NIGHT_NO. Always use the day (light) theme.

MODE_NIGHT_YES. Always use the night (dark) theme.

MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).

(you can apply one of these by checking what values were saved in shared preferences, as in your example)

More info :

https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setDefaultNightMode(int)

https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94

Day/Night theme for android app

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51