Meryl
Meryl

Reputation: 164

Light text in dark status bar is not working in Android 11

I'm changing status bar color and text between fragments/activities. For black status bar I need white text and viceversa. Due to api deprecation on Android 11 I am using this:

Dark status bar with light text:

Window window = getWindow();
    if (mDefaultStatusBarColor == null) {
        mDefaultStatusBarColor = window.getStatusBarColor();
    }
    window.setStatusBarColor(ContextCompat.getColor(this, R.color.black));

    // Sets status text light
    View decorView = window.getDecorView();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowInsetsController wic = decorView.getWindowInsetsController();
        wic.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

Changing to light status bar with dark text:

Window window = getWindow();
            // Giving the status bar its default color.
            if (mDefaultStatusBarColor != null) {
                window.setStatusBarColor(mDefaultStatusBarColor);
            }

            // Sets status text dark
            View decorView = window.getDecorView();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                WindowInsetsController wic = decorView.getWindowInsetsController();
                wic.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }

But the first case (dark status bar with light text) is not working properly because text is still dark.

Did I miss to add something?

Upvotes: 2

Views: 636

Answers (1)

R.Lin
R.Lin

Reputation: 26

since SetSystemUIVisibilty was deprecated in API level 30, use WindowInsetsController instead:

final WindowInsetsController insetsController = getWindow().getInsetsController();
int systemBarsAppearance = insetsController.getSystemBarsAppearance();
// Sets status text light
insetsController.setSystemBarsAppearance(systemBarsAppearance, ~WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
// Sets status bar dark
getWindow().setStatusBarColor(0xff000000);

All the best!

Upvotes: 0

Related Questions