Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

I would like to change the color of highlighted bar in Android Studio:

enter image description here

How can i do it?

like image 452
Humty Avatar asked Sep 06 '16 06:09

Humty


People also ask

How can I change my status bar in Android?

To customize it, first pull down the slider bar from the top of the screen. Next, tap on the three vertical dots in the top right corner. Now click on Status bar. You're in.

How do I change my toolbar color on android?

Just go to res/values/styles.edit the xml file to change the color of action bar. Code for styles.

Why is my status bar black?

A recent update to the Google application caused an aesthetic issue with the font and symbols turning black on the notification bar. By uninstalling, reinstalling, and updating the Google application, this should allow the white text/symbols to return to the notification bar on the home screen.


2 Answers

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); 
like image 60
earthw0rmjim Avatar answered Oct 02 '22 21:10

earthw0rmjim


The status bar is a system window owned by the operating system. On pre-5.0 Android devices, applications do not have permission to alter its color, so this is not something that the AppCompat library can support for older platform versions. The best AppCompat can do is provide support for coloring the ActionBar and other common UI widgets within the application.

On post-5.0 Android devices, 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.

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(activity.getResources().getColor(R.color.my_statusbar_color)); 
like image 42
salik latif Avatar answered Oct 02 '22 22:10

salik latif