4

I an trying to update status bar dynamically as below but getting warning that flag FLAG_TRANSLUCENT_STATUS is depricated now, how i can fix this?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        // clear FLAG_TRANSLUCENT_STATUS flag:
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        // finally change the color
        getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
}
Zain
  • 37,492
  • 7
  • 60
  • 84
sweet_vish
  • 141
  • 1
  • 8

3 Answers3

2

I spent too much hours to get the proper answer, because i have an error with status bar color as well. I will put the answer here if someone has the same problem.

You need to set the status bar color in transparent as you do in the code using the last build version as "if" statement, thats for sure. But, also, you need to set this layout attribute in the root view as well.

android:fitsSystemWindows="false"

Hope this will help you resolving the status bar error in Android 30.

Ruben Caster
  • 290
  • 1
  • 5
  • 15
0
if (Build.VERSION.SDK_INT >= 21) {
    Window window = getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= 19) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    //Virtual keyboard is also transparent
    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
  • 1
    Where did you copy this from? – Dharman Oct 01 '22 at 21:37
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '22 at 10:52
0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  window.setStatusBarColor(Color.TRANSPARENT);
} else {
  window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '22 at 12:59