Support for lollipop devices while using windowLightStatusBar

Priya Sindkar
3 min readJul 26, 2019

--

Android API 23 brought with it ability to make status bar icons dark by setting style item `windowLightStatusBar = true`. This was recently helpful to me when I was using DayNight theme in one of my apps.

Light theme in devices with API ≥ 23
Dark theme in devices with API ≥ 23

Enabling Light status bar with dark icons for light/day theme

Add below style items to res/values/styles.xml file:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"><item name="android:statusBarColor">?android:colorBackground</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>

Enabling Dark status bar with light icons for dark/night theme

Add below style items to res/values-night/styles.xml file:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"><item name="android:windowLightStatusBar">false</item></style>

Adding above styles in your project will take care to display status bar style as per the application wide theme chosen by 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.

Distorted status bar icons in light theme on lollipop devices

The Solution- Set status bar color suitable for both, light and dark themes for API level 21 & 22

Only solution I found to have clearly visible status bar icons in lollipop devices was to manually set 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:

open class BaseActivity : AppCompatActivity() {
....
override fun onCreate(savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
window.statusBarColor = Color.BLACK
}
}
}

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 like on lollipop devices with this solution-

Manually apply black color to status bar on devices running on lollipop (API 21 & 22)

… and this is how the dark theme will look like-

Dark theme look on lollipop (API 21 & 22) devices

That’s it! Just set statusBarColor in your class manually for API < 23 and you are good to go.

Note: Same problem occurs with a distorted navigation bar while using windowLightStatusBar = true. Same solution can be applied to color the navigation bar as needed. i.e-

window.navigationBarColor = Color.BLACK

Most of the 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 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 out the solution!

Hope this helps someone! Happy Coding!

Note: Minimum SDK level for this project is 21. Window methods- setStatusBarColor() and setNavigationBarColor() are not supported on API ≤19.

--

--

Priya Sindkar

Sr. Android Developer 💚 | Believer in neat code and clean architecture | Loves to read | A coder through and through 👩🏻‍💻