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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.pink)); //here
}

int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
       | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
getWindow().getDecorView().setSystemUiVisibility(flags);

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 (R.color.pink) 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.