Code for setting light status bar in android 11 and below android 11 deprecation warning not going

Viewed 3117
 Window window = MainActivity.this.getWindow();
                   window.setStatusBarColor(MY_COLOR_IT_CAN_BE_ANY);

                   if (Build.VERSION.SDK_INT < 30)
                   {
                       window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                   }
                   else {
                       window.setDecorFitsSystemWindows(false);
                       WindowInsetsController controller = getWindow().getInsetsController();
                       if(controller != null) {
                           controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                                   WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
                       }
                   }

I am using this code for setting light status bar in android 11 and below android 11. Everything works fine, just a little problem, deprecation warning not going.

3 Answers

deprecation warning is showing when you use deprecated code and you are not - you have proper if statemnent, causing using not-yet-deprecated methods on < 30 and new method on 30+

if(fullScreen)
{
    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
else
{
    getWindow().getDecorView().getWindowInsetsController().show(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_BARS_BY_SWIPE);
}

Above code used for full screen from Android 11, it's important to set setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE) else on swipe down status bar get display and does not disappear till activity is not recreated.

I just did one thing, since my if statement is fine, I just suppressed warning by using @SuppressWarnings("deprecation")

I just used this annotation for that particular method which contains this code.

There is another option present which is also perfectly fine: just add

//noinspection deprecation

above line of code which is deprecated. This will allow to check other warnings in that whole function. Which is good and I will prefer.

What is the advantage of //noinspection deprecation over @SuppressWarnings("deprecation")?

This prevents the problem with @SupressWarnings, which is it ignores all warnings in the method. So if you have something deprecated that you are not aware of, @SupressWarnings will hide it and you will not be warned. That is the advantage of the //noinspection

Courtest and check more details in this post answers: How to suppress specific Lint warning for deprecated Android function?

Related