Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically setting & removing TRANSLUCENT Status bar flag

I'm developing app which runs on Android Lollipop. I have set the status bar color using the following code.

First I set this flag WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS & then...

getWindow().setStatusBarColor(Color.RED);

But my app requires dynamically set FLAG_TRANSLUCENT_STATUS but when I set this flag, it perfectly works showing Transparent Status Bar. Later when I want to color the status bar back, the WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag doesn't work

like image 467
Akshay Chordiya Avatar asked Dec 14 '25 09:12

Akshay Chordiya


2 Answers

Use the following to add or remove FLAG_TRANSLUCENT_STATUS flag:

window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
like image 114
fahmy Avatar answered Dec 16 '25 09:12

fahmy


Coloring the status bar is enabled as default when your app is running on Lollipop and your activity theme inherits from Theme.AppCompat.Light.DarkActionBar.

AFAIK the FLAG_TRANSLUCENT_STATUS is only necessary to color the status bar on Android KitKat.

So in your case, you can simply use

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(yourColor);
}

in your activity with any color you like (including black of course ;) )

like image 44
Daniel Veihelmann Avatar answered Dec 16 '25 07:12

Daniel Veihelmann