Reputation: 33438
If night mode is ON, I want to pick a different layout in my activity.
What I have done:
added separate layout folders for night
and notnight
as follows:
res
-----layout-night
----------my_layout.xml
-----layout-notnight
----------my_layout.xml
Now, when I toggle night mode and reopen the app, I was hoping the layout would change, but it doesn't.
Is there something extra required for this to work?
Is there a permission i need to add for this to work? If yes, which one?
Not looking for an alternate approach. My use case specifically needs a layout to be picked based on night mode status. I'm trying to figure out why the right layout doesn't get picked automatically, when the folders are in place.
UPDATE : Narrowed down problem. The layout layout-notnight
gets picked always. Hence, folder structure is working fine. App is not being able to detect the night mode. (Night mode is toggled ON on the device- tried toggling on and off it works on device but app wont detect)
So question now is:
Is a permission required for it being able to detect the state of night mode? like it needs for detecting wifi state and network state.
Upvotes: 3
Views: 1371
Reputation: 195
If you are using a toggle button or checkbox, then you can store a Boolean value in SharedPreferences and can use to that to check whether the user wants night mode or not. This would work even after the app is closed and used again.
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("dark", isChecked);
editor.commit();
}
});
Then, in the next activity
SharedPreferences preferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(preferences.getBoolean("dark",false) == true)
adapter = new ArrayAdapter<String>(this,R.layout.list_item_dark,name);
else
adapter = new ArrayAdapter<String>(this,R.layout.list_item,name);
Similarly, you can change layout using this method.
Upvotes: 0
Reputation: 3340
The documentation says :
On API 22 and below, changes to the night mode are only effective when the car or desk mode is enabled on a device. Starting in API 23, changes to night mode are always effective.
MODE_NIGHT_AUTO automatically switches between night and notnight based on the device's current location and certain other sensors
And from here(notice the BOLD text):
This can change during the life of your application if night mode is left in auto mode (default), in which case the mode changes based on the time of day.
So you can try to adjust the device time to a proper value.
But please also be aware that the night mode detection may as well depend on other certain sensors(which the developer site doesn't say clearly). Guess the light sensor might be taken into consideration.
Upvotes: 1
Reputation: 537
You could try using UiModeManager
. It provide methods setNightMode()
and getNightMode()
. Maybe it not solve the problem but this is the way to do it.
Upvotes: 1