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.