Android API 23 brought with it the ability to make status bar icons dark by setting the style item `windowLightStatusBar = true`. This was recently helpful to me when I was using the DayNight theme in one of my apps.
Enabling the Light status bar with dark icons for a light/day theme
Add the below style items to the res/values/styles.xml file:
Enabling a Dark status bar with light icons for a dark/night theme
Add the below style items to the res/values-night/styles.xml file:
Adding the above styles to your project will take care to display the status bar style as per the application-wide theme chosen by the user.
Problem on Android devices running on Lollipop devices (API 21 & 22):
As you can see, `windowLightStatusBar` is supported only in API 23 and above. For lollipop devices, i.e. API 21 and 22, this theming will create a light status bar with light status bar icons which provides a very bad user experience.
The Solution- Set status bar color suitable for both, light and dark themes for API levels 21 & 22
The only solution I found to have visible status bar icons in lollipop devices was to manually set the status bar color for API levels below 23.
In my BaseActivity class’s onCreate() method, which all other activities in my app inherit from, I add the following:
Now, my app will show a black-colored status bar for light and dark themes alike and will not provide an unpleasant user experience.
This is how the light theme will look on lollipop devices with this solution-
… and this is how the dark theme will look like-
That’s it! Just set statusBarColor in your class manually for API < 23 and you are good to go.
Note: The same problem occurs with a distorted navigation bar while using windowLightStatusBar = true. The same solution can be applied to color the navigation bar as needed. i.e-
Most Android developers are aware of windowLightStatusBar and `window.statusBarColor` features which were introduced long back in APIs 23 and 21 respectively. But since Android Q’s support for the dark theme is been used by many, I felt it was needed to learn how to give support for lollipop devices when using `windowLightStatusBar = true`. Particularly when I had to invest 2–3 hours to find the solution!
Hope this helps someone! Happy Coding!
Note: The minimum SDK level for this project is 21. Window methods- setStatusBarColor() and setNavigationBarColor() are not supported on API ≤19.
For more support & information connect with us on https://www.novumlogic.com/