3
 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.

Amy
  • 1,114
  • 13
  • 35
Chaitanya Karmarkar
  • 1,425
  • 2
  • 7
  • 18

3 Answers3

0

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+

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • So why warning is not going if 'if statement' is proper? – Chaitanya Karmarkar Sep 14 '20 at 07:11
  • because why it would warn you if you are using code properly? remove `if` statement and use `setSystemUiVisibility` on all OS versions, then you will get warning, because you will intend to use deprecated code (`setSystemUiVisibility` call od API 30, when it is deprecated). in short: your `if` statement is fixing warning properly – snachmsm Sep 14 '20 at 07:31
  • Nope it also gives warning if I use 'if statement'. And warning is still there if 'if statement' is removed. – Chaitanya Karmarkar Sep 14 '20 at 11:55
  • I just suppressed that warning look at my answer. – Chaitanya Karmarkar Sep 14 '20 at 12:08
  • I can't check now if I can reproduce your warning, but if it's possible then this is a bug in SDK and should be fixed (soon?) in next update. hiding this bug by `noinspection` is only temporary solution and may lead to leave this line in code forever, and next year may hide another important deprecation – snachmsm Sep 14 '20 at 14:20
  • Yep you are correct. Since if statement is proper, until any possible workaround I will suppress warning to get rid of it. – Chaitanya Karmarkar Sep 15 '20 at 02:43
0
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.

MJM
  • 5,119
  • 5
  • 27
  • 53
-1

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?

Chaitanya Karmarkar
  • 1,425
  • 2
  • 7
  • 18