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.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • 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 Jul 08 '16 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. – Viral Patel Jul 08 '16 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 Jul 08 '16 at 20:40
  • @Vucko : yup, you got it right – Viral Patel Jul 08 '16 at 20:41
  • 1
    dont need to mimic, the documentation says it is supported. Hence the curiosity :) https://developer.android.com/guide/topics/resources/providing-resources.html – Viral Patel Jul 08 '16 at 20:46
  • 1
    Ah I see. Can you verify that the night-mode is activated by using `getNightMode()` and logging the value? – Vucko Jul 08 '16 at 20:56
  • Good catch, I tried that and it says not in night mode. Although it actually is in night mode. Thanks for narrowing down the problem area. Now need to focus on why the app is not detecting that it is on. – Viral Patel Jul 08 '16 at 21:01
  • any chance it would require a permission to be added in the manifest? – Viral Patel Jul 08 '16 at 21:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116838/discussion-between-vucko-and-androidmechanic). – Vucko Jul 08 '16 at 21:08

3 Answers3

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.

  • Thanks for the response. That i know. But I'm basically trying to find out why the right layout wont get picked up automatically. – Viral Patel Jul 08 '16 at 20:33
  • Did you try using `UiModeManager.setNightMode(MODE_NIGHT_AUTO)` in your code? – Vladislav Sazanovich Jul 08 '16 at 20:42
  • I'm on Cyanogenmod 13. I can toggle it from quick settings. – Viral Patel Jul 08 '16 at 20:43
  • Look at the comments on this post: http://stackoverflow.com/questions/5002931/change-mode-of-android-phone-as-day-night-automatic?rq=1 It sounds strange but night/nonnight mode available only if android is in the car mode. – Vladislav Sazanovich Jul 08 '16 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. – Viral Patel Jul 08 '16 at 21:02
  • Maybe my last try...(I really have no idea) Just for experiment. Put different `my_layout.xml` in layout folder and see what is going on. – Vladislav Sazanovich Jul 08 '16 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.

suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • 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? – Viral Patel Jul 13 '16 at 07:32
  • Some certain sensors, but the doc doesn't specify which sensor it will use – suitianshi Jul 13 '16 at 07:34
  • but do you know which sensors? is there an external source/reference for this information? – Viral Patel Jul 13 '16 at 07:36
  • you can read the accepted answer in http://android.stackexchange.com/questions/83810/what-is-android-night-mode-what-triggers-it-and-what-are-the-consequences as additional information. And the answer to ur question, sorry, I don't. – suitianshi Jul 13 '16 at 07: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.

Aman Vangani
  • 175
  • 1
  • 1
  • 9
  • 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... – Viral Patel Jul 18 '16 at 04:32