How to use setStatusBarColor Method In Android

This method can be only used in the above API Level 21. Officially status bar color is not supporting below API Level 21. Although, Here we added an if condition, because in case if you haven’t selected above or equal to API 21 then it will check the android API Version, and then it will execute the code. It will not change the color of the status bar is below API Level 21 but the rest code will work well.

Step 1: Create a New Projects

After opening the android studio and creating a new project with an empty activity.

Step 2: Update and Add Color in the colors.xml file

Navigate to res/values/colors.xml, and add a color that you want to change for the status bar. Add the color mentioned below:

<color name="colorPrimaryDark">#3700B3</color>

Step 3: Add Code Snippet to Update Status Bar Color

In your MainActivity, add this code in your onCreate method. Don’t forget to replace your desired color with colorName

Java
if (Build.VERSION.SDK_INT >= 21) {
            Window window = this.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
        }
Kotlin
if (Build.VERSION.SDK_INT >= 21) {
            val window = this.window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.statusBarColor = this.resources.getColor(R.color.colorPrimaryDark)
        }


Step 4: Try running your application on an android emulator or a physical device. See the changes.




How to Change the Color of Status Bar in an Android App?

A Status Bar in Android is an eye-catching part of the screen, all of the notification indications, battery life, time, connection strength, and plenty of things are shown here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of the design that the color of the status bar should follow the color combination of the layout. You can look out at many Android apps on your phone and can see how they changed it according to their primary colors.

Similar Reads

Methods to Change the Color of the Status Bar

There can be multiple ways for changing the status bar color but we are going to tell you about the best hand-picked two methods which you can use either in Java or Kotlin. The two easy methods are mentioned below:...

Creating a New Theme

You can follow this method in apps that are built with Kotlin or Java. It will work in both....

Using setStatusBarColor Method

This method can be only used in the above API Level 21. Officially status bar color is not supporting below API Level 21. Although, Here we added an if condition, because in case if you haven’t selected above or equal to API 21 then it will check the android API Version, and then it will execute the code. It will not change the color of the status bar is below API Level 21 but the rest code will work well....