Android - default dark mode - java

I want to implement in my app Dark mode. In default I wish it been following by the system, so in Main Activity I've placed:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
It works fine, but I if user wants to change its mind and select certain option in my app menu to toggle off/on dark mode, activity is restarting and app's still following system rules. How can I change that?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_color_mode) {
if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
else
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
return true;
}

Code responsible for option you mentioned, is within onCreate(). Mechanism that allows user to change mode is not within onCreate()
public class MainActivityextends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
When you explicitly change the dark mode, Android recreates the activity and hence calls onCreate again.
So, after you change the dark mode you won't notice a change, as AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM is called again when onCreate is called by the system.
To make this works you can save a value into SharedPreference that can be checked in onCreate before setting the system dark mode.
This can be a boolean that you can toggle when you want to manually change the dark mode.
Here is a sample
public class MainActivityextends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
if (isSystem) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_color_mode) {
if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
else
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("IS_SYSTEM", false).apply();
return true;
}
}
UPDATE
that works perfect, but when I quit application and then launch again, default system mode is active although I've switched it. Is possible here to make it works in that way?
You can use another SharedPreference boolean to be saved permanently
public class MainActivityextends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
boolean isNight = prefs.getBoolean("IS_NIGHT", false);
if (isSystem) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else if (isNight) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_color_mode) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
prefs.edit().putBoolean("IS_NIGHT", false).apply();
} else {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
prefs.edit().putBoolean("IS_NIGHT", true).apply();
}
prefs.edit().putBoolean("IS_SYSTEM", false).apply();
return true;
}
}

Related

Toggle method not working properly in Android Studio using JAVA

I made simple dark/light mode switcher. The application OnCreate detects the mode you are on and than changes the boolean. I'm toggling this boolean and theme with button but for some reason it changes to false after second click. It only does that when the boolean is true. On false it works fine. Here's my code:
ImageButton dark_light;
boolean isDarkMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dark_light = findViewById(R.id.dark_light);
int nightModeFlags =
getApplicationContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
dark_light.setImageResource(R.drawable.light);
isDarkMode = true;
break;
case Configuration.UI_MODE_NIGHT_NO:
dark_light.setImageResource(R.drawable.dark);
isDarkMode = false;
break;
}
dark_light.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isDarkMode) {
dark_light.setImageResource(R.drawable.light);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isDarkMode = true;
} else {
dark_light.setImageResource(R.drawable.dark);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isDarkMode = false;
}
Log.d("Dark_Light", String.valueOf(isDarkMode));
}
});
}
Whenever night mode is toggled, onCreate will be called again.
getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; always gives you the initial configuration so you are always writing the same value to isDarkMode whenever the night mode is toggled.
To get the current night mode configuration, use AppCompatDelegate.getDefaultNightMode() instead
int nightModeFlags = AppCompatDelegate.getDefaultNightMode();
switch (nightModeFlags) {
case AppCompatDelegate.MODE_NIGHT_YES:
dark_light.setImageResource(R.drawable.light);
isDarkMode = true;
break;
case AppCompatDelegate.MODE_NIGHT_NO:
dark_light.setImageResource(R.drawable.dark);
isDarkMode = false;
break;
}

App freezes after PreferenceSwitch in settings Android Studio

I made settings activity from Android Studio, not manually. I want to make a switch that applies a dark mode in the app. The problem is that when I click the switch there is no animation for movement, only a blink from the app. When I go back in my Main Activity I see that the theme is applied(I have tried earlier only with the main activity), but when I try to go back in the settings everything is frozen! Nothing can be clicked, no reaction at all.
This is my Java code:
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
public class SettingsActivity extends AppCompatActivity {
private SwitchPreference darkModeSwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
SwitchPreference darkModeSwitch = (SwitchPreference) findPreference("darkmode");
assert darkModeSwitch != null;
darkModeSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
return false;
}
});
}
}
}
This is root_preferences.xml:
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="General">
<SwitchPreference
app:key="darkmode"
app:title="Dark mode"/>
</PreferenceCategory>
And this is the Logcat after applying the theme:
avc: denied { getattr } for path="/proc/1" dev="proc" ino=3924 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=dir permissive=0
I think that it may be because I have not started the activity again after applying the theme, because when I tried the dark mode in main_activity, there were two lines, which in my settings activity cannot be placed, because the class is static... Please help!
public void onClick(View v) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
//These two
finish();
startActivity(new Intent(MainActivity.this, MainActivity.this.getClass()));
}
});
Put this in your preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:defaultValue="false"
app:key="#string/key_night_mode"
app:summaryOff="#string/summary_night_mode_off"
app:summaryOn="#string/summary_night_mode_on"
app:title="#string/title_night_mode" />
</PreferenceScreen>
Then in your SettingsFragment.java which extended PreferenceFragmentCompat inside the onCreatePreference() method:
SwitchPreferenceCompat switchPreferenceCompat = findPreference(getString(R.string.key_night_mode));
switchPreferenceCompat.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean isChecked = false;
if (newValue instanceof Boolean)
isChecked = (Boolean) newValue;
if (isChecked) {
//these lines so that the preference persists
getPreferenceManager().getSharedPreferences().edit().putBoolean(getString(R.string.key_night_mode), true).apply();
//you do not need to finish and recreate activity
//it takes care of any such things that needs to be done
//automatically
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
getPreferenceManager().getSharedPreferences().edit().putBoolean(getString(R.string.key_night_mode), false).apply();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return true;
}
});
These lines of codes are working for me.
Note:
AppTheme should extends from Theme.AppCompat.DayNight or Theme.MaterialComponents.DayNight if you want to be dependent on platform for your night theme. Otherwise, you can create your separate Day and Night theme which extends from Theme.MaterialComponents.Light and Theme.MaterialComponents respectively(AppCompat has similar set of themes too).
Every time the app starts, it needs to respect the user's preferences and start in the user setted theme accordingly. To achieve this, you can check the preference and set theme in the MyApplication class as follows:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean nightMode = sharedPreferences.getBoolean(getString(R.string.key_night_mode), false);
if (nightMode)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}

android.widget.Swtich - Keep state on pause?

I am a new developer writing an app that logs location when a switch is toggled. My switch works when I toggle it, but it loses it's state when I take the app out of the forefront. How do I keep the state of my android switch onPause and onResume?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// set view, initiate switch
Switch mainSwitch = (Switch) findViewById(R.id.mainSwitch);
// deal with switch
mainSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#SuppressLint("MissingPermission")
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if switch is on
if (isChecked) {
logLocation();
}
} else if (!isChecked) {
stopLocation();
}
// true if the switch is in the On position
Log.v("#############=", "" + isChecked);
}
});
}
Adding an answer instead of continuing in the comments.
By default Android will save the state of your UI if and only if you're using default components (meaning TextView, Button, Switch etc.) and your component has an id. This state will only survive if the OS kills the app and the user returns to it. This means that if the user presses the Home button and then returns to your Activity at a later stage, the UI state of all default components of that specific Activity will be restored. When pressing the Home button the Activity is stopped and onSaveInstanceState is called, so here you get a chance to save any state that isn't maintained by default components or if you have some data you downloaded for instance that is used to populate your UI components with data.
If the user presses the Back button the app will be killed and any UI state won't be restored per default.
To overcome this, you can use SharedPreferences or any other local storage mechanism to store your UI state to disk basically.
I've added a snippet of code, that shows how you can use the different approaches.
In your case you don't need the onSaveInstanceState method implementation, as you have a default UI component, that knows how to save its own state already and as mentioned Android does this automagically for you already.
Both states (savedInstanceState and what goes into the SharedPreferences) should be restored in the onCreate method as this is always called whenever an Activity is re-created or created the first time, while onRestoreInstanceState might not be called all the time.
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
private static final String CHECKBOX__STATE = "CHECKBOX__SAVE_STATE";
private Switch checkbox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox = findViewById(R.id.checkbox);
final boolean isChecked;
if (savedInstanceState != null) {
isChecked = savedInstanceState.getBoolean(CHECKBOX__STATE);
} else {
isChecked = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(CHECKBOX__STATE, false);
}
// Setting the state (if any) of the Switch.
checkbox.setChecked(isChecked);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.d(TAG, "onCheckedChanged: isChecked = true - log location");
} else {
Log.d(TAG, "onCheckedChanged: isChecked = false - stop logging location");
}
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(CHECKBOX__STATE, checkbox.isChecked());
}
#Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putBoolean(CHECKBOX__STATE, checkbox.isChecked())
.apply();
}
}
I hope this helps :-)

Android using Shared Preference as pin code

I have an Activity in which I want to ask for a PIN code. In this Activity the user can go to a separate Activity in which he can set the PIN. If no PIN is set, then a default PIN should be used.
This is the code of the first Activity:
public class activity_identification extends AppCompatActivity {
SharedPreferences pins_pref = null;
EditText pin_entry = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_identification);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
pin_eingabe = (EditText) findViewById(R.id.et_pin_entry);
pins_pref = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = pins_pref.edit();
edit.putString("default_pin", "0000");
edit.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pin_identifikation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_change_pin) {
startActivity(new Intent(this, pin_settings.class));
return true;
}
else if (id == R.id.home) {
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickPinSummit(View button){
if(pin_entry.getText().toString().equals( pins_pref.getString("user_pin", null).toString())) {
Toast.makeText(this, "Authorization Right", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Authorization Wrong", Toast.LENGTH_SHORT).show();
}
}
}
And this is the second Activity:
public class pin_settings extends AppCompatActivity {
EditText et_old_pin = null;
EditText et_new_pin = null;
EditText et_new_pin_repeat = null;
Button btn_pin_save = null;
TextView tv_sharedPIN = (TextView) null;
SharedPreferences pins_prefs = null;
private String default_pin = null;
private String user_pin = null;
private String old_pin = null;
private String new_pin = null;
private String new_pin_repeat = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pin_einstellungen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
et_old_pin = (EditText) findViewById(R.id.et_old_pin);
et_new_pin = (EditText) findViewById(R.id.et_new_pin);
et_new_pin_repeat = (EditText) findViewById(R.id.et_new_pin_repeat);
btn_pin_save = (Button) findViewById(R.id.btn_pin_save);
tv_sharedPIN = (TextView) findViewById(R.id.sharedPIN);
pins_prefs = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE);
general_pin = pins_prefs.getString("default_pin", null);
tv_sharedPIN.setText(pins_prefs.getString("general_pin", null));
}
public void onClickNewPinSave(View button){
old_pin = et_old_pin.getText().toString();
new_pin = et_new_pin.getText().toString();
new_pin_repeat = et_new_pin_repeat.getText().toString();
if(old_pin.equals(pins_prefs.getString("default_pin",null).toString())){
if (new_pin.equals(new_pin_repeat)){
SharedPreferences.Editor edit = pins_prefs.edit();
edit.putString("user_pin", user_pin);
edit.commit();
tv_sharedPIN.setText(pins_prefs.getString("user_pin", null));
} else {
Toast.makeText(this, "New Pins are not the same", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Old Pin is wrong.", Toast.LENGTH_LONG).show();
}
}
}
Now the problem is that the app does not save the PIN which is set in the second Activity. It works as long as the app is open, but as soon as I restart it the PIN reverts back to the default PIN. I think this is because I set the default PIN in the onCreate() method of the first Activity, but I just can't figure out how to get around doing that.
Of course the PIN will always be reset to the default since you set it each time on startup. All you need to do to fix that is remove that line and instead use the default value which is passed into getString() correctly.
You don't need to set the default PIN at all! Remove all of that from onCreate()
getString() accepts a default value as second parameter. This default value is returned if no value is set. Let me repeat that: The default value is return if no value has been set.
So instead of setting the default PIN in onCreate() you can simply pass in the default PIN into getString() and if no PIN has been set, getString() will then return the default PIN. So what you are looking for is this:
String pin = preferences.getString("default_pin", "0000");
On a side note: Please adhere to the common Java naming conventions. Also don't hardcode so many Strings. Use constants instead.
EDIT: As Xaver Kapeller pointed out my earlier solution wont work. Although his one is simpler, i'll fix mine just because
Set default_pin to 0000 if getString(default_pin, "-1") returns "-1".
OLD: Try only setting the default_pin to 0000 if default_pin is null, which will only happen at the very first launch of your activity

Android - remember me function for saving radio buttons in Java

I am new to Android development and I am wondering if there is a way to have some sort of remember me function where it allows a user to have an option whether to save the current state of radio buttons.... a bit like a remember me function on a login apart from using a set of radio buttons instead....any help will be appreciated!
Here is my code:
#Override
/*
* Holding the data for the radio buttons from the xml file
*/
RadioGroup gender = (RadioGroup) findViewById(R.id.question1);
gender.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.answer1A:
ans1 = 1;
break;
case R.id.answer1B:
ans1 = 2;
break;
}
}
});
To obtain shared preferences, use the following method In your Code:
SharedPreferences _prefs = this.getSharedPreferences(
"values_to_remember", Context.MODE_PRIVATE);
To read preferences:
int ansA = "ans1";
int value = _prefs.getInt(ansA, 1(default value));
To edit and save preferences
_edit().putInt(ansA , 1).apply();
private RadioButton button;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences=getSharedPreferences("check", MODE_PRIVATE);
button=(RadioButton)findViewById(R.id.question1);
button.setChecked(preferences.getBoolean("set", false));
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("set", isChecked);
editor.commit();
}
});
}
You will have use a sharedpreference. And store the state of radiobutton in it.

Categories