How to change the color of status bar? [Solution] (Android Development)

status bar color changeChanging the color of the status bar is available only for devices with Android Lollipop(API 21) and higher.
Therefore we would have to add a line of code that would check if the android version is at lease 21 or not and then try to change the color of the status bar.

Color of the status bar can be changed both programatically(using Java) and through XML.

In this article I would show how to do it in a way such that your app can still run on lower devices without crashing but on devices with sdk 21 and higher it will change the color of the status bar.

  • Using JAVA:
    Just add the following snippet of code to the OnCreate Method of the Activity whose status bar you want to change:

    //changing statusbar color
            if (android.os.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.your_color));
            }
    

    Such that the Activity looks like:
    status bar color change 1

    Note: There is a flaw in this method. That is, you need to add this code to all the Activities of your App which is quite tiresome if you have a lot of Activities.

  • Using XML:
    Another way you can change the color of the status bar is through styles.xml.
    status bar color change 2

The advantage of this method is that you don’t need to set the color of status bar for every activity.
However, some designers may want different colors for different activities in which case the first method would be useful.

Well, that’s it. Pretty easy Right?!
If you have any questions or doubts just drop them in the comments section down below.

Hey! I am a Physics Major at the University of Delhi.
I dream of being a Theoretical Physicist one day. I love playing Guitar in my free time. I can speak English, Hindi and a little French. I am also a Web Development Enthusiast and a hardcore Gamer.

Leave a Reply

Your email address will not be published. Required fields are marked *