Is it possible to change the color of the upper part of the action bar? As shown in the attached image
-
That is the status bar, not part of the action bar. In a typical Android Studio project, that is controlled by your `colorPrimaryDark` attribute on the style that you are using. See https://developer.android.com/guide/topics/ui/look-and-feel/themes#CustomizeTheme for more. – CommonsWare Aug 30 '20 at 22:08
-
Yes But I want to do that by code. Because I need to change the color during the app running. – Julián Oviedo Aug 30 '20 at 23:07
-
3Does this answer your question? [How to change the color of the status bar in Android?](https://stackoverflow.com/questions/39341818/how-to-change-the-color-of-the-status-bar-in-android) – Will Aug 31 '20 at 00:55
2 Answers
Yes, you can. As @CommonsWare has said you can change the color of the StatusBar by editing the colorPrimaryDark
item in your styles resource file.
Screenshot of what it looks like in Android Studio
To change the StatusBar Color through Java Code, you can add the following code to your activity. Note that the Android Device must be running on API 21
or higher.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.darkPurple));
}
Also, if you want to change the foreground text of the StatusBar, you can just add the following code to the if
block. Note that the Android Device must be running on API v23
or higher.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// for a black color foreground text:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
// for a white color foreground text, just replace setSystemUiVisibility param with ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}

- 25
- 1
- 7
-
Yes But I want to do that by code. Because I need to change the color during the app running. – Julián Oviedo Aug 30 '20 at 23:06
-
First of all this is the Status bar not the Action bar. However, if you want to change the statusbar color grammatically you can use Window.setStatusBarColor()
. But the device should have android 5.0
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}
Should work for you.

- 323
- 4
- 13