How to Change the Status Bar Color in Android

How to change the status bar color in Android?

Update:

Lollipop:

public abstract void setStatusBarColor (int color)

Added in API level 21

Android Lollipop brought with it the ability to change the color of status bar in your app for a more immersive user experience and in tune with Google’s Material Design Guidelines.

Here is how you can change the color of the status bar using the new window.setStatusBarColor method introduced in API level 21.

Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag and clear the FLAG_TRANSLUCENT_STATUS flag.

Working Code:

import android.view.Window;

...

Window window = activity.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(activity,R.color.my_statusbar_color));

Offcial developer reference : setStatusBarColor(int)

Example :material-design-everywhere

Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!

Sample Image

The transitionName for the view background will be android:status:background.

How to change the color of the status bar in Android?

You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.

(android:statusBarColor inherits the value of android:colorPrimaryDark by default)

For example (since we're using an AppCompat theme here, the android namespace is omitted):

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/your_custom_color</item>
</style>

On API level 21+ you can also use the Window.setStatusBarColor() method from code.

From its docs:

For this to take effect, the window must be drawing the system bar
backgrounds with
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS must not be set.
If color is not opaque, consider setting
View.SYSTEM_UI_FLAG_LAYOUT_STABLE and
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.

To set these flags you could do something like this:

// getWindow() is a method of Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

how to change statusbar color in jetpack compose?

Google has just created a library called accompanist.

You can find it here: https://github.com/google/accompanist

It contains multiple helpful libraries for Jetpack Compose, among which is a System UI Controller that you can use for changing the status bar color.

Docs - https://google.github.io/accompanist/systemuicontroller/

Change status bar text color when primaryDark is white

I'm not sure what API level your trying to target, but if you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:

<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>

when android:windowLightStatusBar is set to true, status bar text color will be able to be seen when the status bar color is white, and vice-versa
when android:windowLightStatusBar is set to false, status bar text color will be designed to be seen when the status bar color is dark.

Example:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Status bar stuff. -->
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>
</style>

How to change status bar color of native splash screen in flutter?

Solved by adding below code in values/styles.xml and values-night/styles.xml in res folder:

  <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
the Flutter engine draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
<item name="android:forceDarkAllowed">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar">true</item>
</style>

How to change status bar icons color in Android?

You can't actually change the status bar icon color in android like that. You can still change the status bar color to whatever you want, but for status bar icons, there are only 2 options - Light (white) or Dark (gray).

If you want to use white icons, then simply put <item name="android:windowLightStatusBar">false</item> attribute in your base application theme. For dark icons <item name="android:windowLightStatusBar">true</item>

You can also do it programmatically like mentioned here - https://stackoverflow.com/a/39596725/10357086

How to match status bar and navigation bar colors to action bar and bottom navigation menu?

If I got you right, you can try to do this programmatically by inserting these lines in your onCreate() method in MainActivity class.

if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.your_color));
getWindow().setNavigationBarColor(ContextCompat.getColor(this,R.color.your_color));
}


Related Topics



Leave a reply



Submit