How to Change the Status Bar Color in Android

How to Change the Status Bar Color in Android

6 June 2024 Stephan Petzl Leave a comment Tech-Help

Changing the status bar color in Android can enhance the visual appeal of your application and create a more immersive user experience. This guide will walk you through the steps to achieve this, ensuring compatibility across different Android versions.

Using Material Design Theme

Starting with Android 5.0 Lollipop, Google introduced the Material Design theme, which allows you to easily customize the status bar color using the colorPrimaryDark attribute in your theme. Here’s how you can set it up:

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_secondary</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="android:statusBarColor">@color/color_primary</item>
  </style>
</resources>

  

Setting Status Bar Color Programmatically

If you need to change the status bar color dynamically, you can use the setStatusBarColor method introduced in API level 21. Below is an example of how to achieve this:

import android.view.Window;
import android.view.WindowManager;
import androidx.core.content.ContextCompat;

// Inside your activity
Window window = getWindow();

// Clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// Finally change the color
window.setStatusBarColor(ContextCompat.getColor(this, R.color.my_statusbar_color));

  

Support for Pre-Lollipop Devices

For devices running versions of Android prior to Lollipop, you can use the support-v7-appcompat library. This library backports many of the Material Design features, including the ability to change the status bar color.

For more details on using the AppCompat library, refer to our comprehensive guide on virtual test devices.

Kotlin Example

If you are using Kotlin, the process is even more straightforward. Here’s a one-liner to change the status bar color:

window.statusBarColor = ContextCompat.getColor(this, R.color.color_name)

  

Practical Example

Let’s consider a practical example where you want the status bar color to match the navigation bar color. You can achieve this by setting both colors in your theme file:

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primary_dark</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="android:statusBarColor">@color/color_primary</item>
    <item name="android:navigationBarColor">@color/color_primary</item>
  </style>
</resources>

  

Conclusion

Changing the status bar color in Android is a simple yet effective way to enhance your app’s user interface. Whether you choose to set the color programmatically or through themes, the methods outlined in this guide will help you achieve the desired effect.

For mobile developers looking to streamline their testing process, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests quickly and efficiently. By leveraging computer vision and AI, Repeato enables developers to focus on creating great products instead of spending time on testing.

For more information on how Repeato can assist in your development process, visit our documentation page.

Like this article? there’s more where that came from!