Changing color of the status bar - (Android Essentials: XML & Java)

   getWindow().setStatusBarColor(ContextCompat.getColor(this,; //here


Enhance your Android app's aesthetic appeal by customizing the color of the status bar to fit your theme. Utilize the provided code snippet to alter the status bar's color to a predefined shade of pink. The code is designed for use within an Activity, where this refers to the current Activity context. The snippet first checks if the Android build version is Lollipop (API level 21) or higher, as status bar color modification is only supported on Lollipop and newer versions. Upon confirmation, it invokes the setStatusBarColor method on the current window, passing in a color resource.

The color resource ( must be defined in your app's res/values/colors.xml file. You can replace pink with any other color name that you have defined.

Additionally, the code includes flags to hide the navigation bar and enable immersive mode, allowing your app to use the full screen. The immersive mode is sticky, meaning it will re-hide the system bars shortly after user interaction.

To incorporate this into your app, follow these steps:

  1. Ensure you have the color defined in your res/values/colors.xml file.

  2. Place the code snippet inside your Activity's onCreate method or any other place where you need to change the status bar color.

  3. Compile and run your app. You should see the status bar color change to the color you defined when the Activity starts.

Remember, this code will only affect the Activity in which it is implemented. If you want to change the status bar color across different Activities, you'll need to include this snippet in each one.