I've just started Android studio for about a month now and come across a lot of problems but always have managed to find a solution. This time I've red and tried a lot of things but now for 4 days I can't proceed, so finally I've decided to ask for help of the community.
What I'm trying to do is save nightmode and switch state in java. So all I got right now is a switch and darkmode on my settings.java
AppCompatDelegate.setDefaultNightmode(AppCompatDelegate.MODE_NIGHT_YES);
So far I figured out that you can save, write and read from sharedPreferences
//Save
SharedPreferences savePreferences = getSharedPreferences("filename", MODE_PRIVATE);
SharedPreferences.Editor editor = savePreferences.edit();
editor.putInt("value", Nightmode);
editor.apply();
//read
SharedPreferences savePreferences = getSharedPreferences("filename", MODE_PRIVATE);
savePreferences.getInt("value", Nightmode);
Already red the android docs but it didn't help, tried multiple tutorials, checked other topics but they did not provide the solution.
I tried putting AppcompatDelegate inside an int to use it inside SharedPreferences but I can't seem to get the full string/int inside it so I can work with it. Tried it with the switch and it worked but then I can't get it to work with Nightmode.
Help would be appreciated !! Here is my settings.java
public class Settings extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setTitle("settings");
SwitchCompat switch2 = findViewById(R.id.switch2);
// saves switch state + night mode
SharedPreferences savePreferences = getSharedPreferences("saveState", 0);
switch2.setChecked(savePreferences.getBoolean("value",false));
int night = savePreferences.getInt("NightModeInt", 2);
AppCompatDelegate.setDefaultNightMode(night);
switch2.setOnClickListener(v->{
if (switch2.isChecked()) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
//save
SharedPreferences.Editor edit = savePreferences.edit();
edit.putBoolean("value", true);
edit.putInt("NightModeInt", 2 );
edit.apply();
} else {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
SharedPreferences.Editor edit = savePreferences.edit();
edit.putBoolean("value", false);
edit.putInt("NightModeInt", 1 );
edit.apply();
}
});
}
}
(This is now working !!)
public class Settings extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setTitle("settings");
SwitchCompat switch2 = findViewById(R.id.switch2);
// saves switch state + night mode
SharedPreferences savePreferences = getSharedPreferences("saveState", 0);
switch2.setChecked(savePreferences.getBoolean("value",false));
int night = savePreferences.getInt("NightModeInt", 2);
AppCompatDelegate.setDefaultNightMode(night);
switch2.setOnClickListener(v->{
if (switch2.isChecked()) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
//save
SharedPreferences.Editor edit = savePreferences.edit();
edit.putBoolean("value", true);
edit.putInt("NightModeInt", 2 );
edit.apply();
} else {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
SharedPreferences.Editor edit = savePreferences.edit();
edit.putBoolean("value", false);
edit.putInt("NightModeInt", 1 );
edit.apply();
}
});
}
}