3

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.

9
  • Can't you place them in the same folder, and just suffix them with 'day' and 'night' and then in onCreate use setContentView to select the appropriate one?
    – Vucko
    Commented Jul 8, 2016 at 20:28
  • No, I know i can do it that way. But that is not my use case. Primary intention is to change the layout based on the status of night mode. Commented Jul 8, 2016 at 20:29
  • Ah ok, sorry. I realize what you're trying to do - something similar to what android does with mdpi, hdpi, xhdpi etc. Maybe you can find out how that's being done and try to mimic that?
    – Vucko
    Commented Jul 8, 2016 at 20:40
  • 1
    dont need to mimic, the documentation says it is supported. Hence the curiosity :) developer.android.com/guide/topics/resources/… Commented Jul 8, 2016 at 20:46
  • 1
    Ah I see. Can you verify that the night-mode is activated by using getNightMode() and logging the value?
    – Vucko
    Commented Jul 8, 2016 at 20:56

3 Answers 3

1

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.

6
  • Thanks for the response. That i know. But I'm basically trying to find out why the right layout wont get picked up automatically. Commented Jul 8, 2016 at 20:33
  • Did you try using UiModeManager.setNightMode(MODE_NIGHT_AUTO) in your code? Commented Jul 8, 2016 at 20:42
  • I'm on Cyanogenmod 13. I can toggle it from quick settings. Commented Jul 8, 2016 at 20:43
  • Look at the comments on this post: stackoverflow.com/questions/5002931/… It sounds strange but night/nonnight mode available only if android is in the car mode. Commented Jul 8, 2016 at 20:56
  • sorry, didnt try your first suggestion in comment earlier. Did now. Turns out the app is not detecting the night mode although on the device the mode is on and i can see the different in the tint on screen when on. Commented Jul 8, 2016 at 21:02
1

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.

4
  • I'm on API Level 23 and have tried all settings - auto as well as night explicit. App just wont detect. what are the other factors you say the detection will depend on? Commented Jul 13, 2016 at 7:32
  • Some certain sensors, but the doc doesn't specify which sensor it will use
    – suitianshi
    Commented Jul 13, 2016 at 7:34
  • but do you know which sensors? is there an external source/reference for this information? Commented Jul 13, 2016 at 7:36
  • you can read the accepted answer in android.stackexchange.com/questions/83810/… as additional information. And the answer to ur question, sorry, I don't.
    – suitianshi
    Commented Jul 13, 2016 at 7:40
0

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.

1
  • Thanks for the response but a workaround s not what i'm looking for. I know how to get workarounds. The answer I'm looking for is when the night mode is on at device level, why wont the app detect it... Commented Jul 18, 2016 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.