Android Lollipop Set Status Bar Text Color
Asked Answered
P

8

40

I'm trying to set the status bar text color in Android v21, but I'm not seeing an API method for it. Here's what I have so far for the background

MyActivity.java > onCreate:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        window.setStatusBarColor(getResources().getColor(R.color.white));

    }

Obviously white text on white background won't work. I'm looking for something like

window.setStatusBarTextColor(getResources().getColor(R.color.orange));
Playbill answered 26/5, 2015 at 16:36 Comment(3)
i don't think,there no method like setStatusBarTextColor in Android 5.0Biles
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.Paintbrush
Changing Lollipop status bar text color is not possible. Either you set the status bar background to be translucent which results in #4000 background guaranteeing sufficent legibility or you set it manually to some color (hint: primary dark, keyword dark) which will guarantee enough contrast with the white foreground. We can now manually change the color from black or whatever random drawable Samsung was using, isn't that enough?Laconic
B
10

For Android 6.0 and above You can check Sunita's answer.

Android 5.0

we are able to change only status bar color not status bar text color.

there is no method like

setStatusBarTextColor(getResources().getColor(R.color.orange));

In short, it's not possible on Android 5.0. Check this answer

Biles answered 26/5, 2015 at 16:43 Comment(2)
It is possible from API 23 and above, check the answer below by SunitaHindorff
Lol, i just copypasted this code, even didn't read that there is no method like ...Snowblink
A
82

You can not set the status bar text color by specifying any color explicitly

But you can try below alternative which is Added in API 23,

You can use "android:windowLightStatusBar" attribute in two ways

  • "android:windowLightStatusBar" = true, status bar text color will be compatible (grey) when status bar color is light
  • "android:windowLightStatusBar" = false, status bar text color will be compatible (white) when status bar color is dark
<style name="statusBarStyle" parent="@android:style/Theme.DeviceDefault.Light">
        <item name="android:statusBarColor">@color/status_bar_color</item>
        <item name="android:windowLightStatusBar">false</item>
</style>

You can check above api in below link - https://developer.android.com/reference/android/R.attr.html#windowLightStatusBar

Anagram answered 3/11, 2015 at 21:10 Comment(2)
Amazing. didn't know that. Works like a charm.Dysgraphia
In case anyone else is just a 'maintainer' of an Android app like me; just drop this in your styles.xml under the theme section your app or view is using: <item name="android:windowLightStatusBar">true</item>Ketch
E
24

Here's a Java implementation of Gandalf458's answer.

/** Changes the System Bar Theme. */
@RequiresApi(api = Build.VERSION_CODES.M)
public static final void setSystemBarTheme(final Activity pActivity, final boolean pIsDark) {
    // Fetch the current flags.
    final int lFlags = pActivity.getWindow().getDecorView().getSystemUiVisibility();
    // Update the SystemUiVisibility dependening on whether we want a Light or Dark theme.
    pActivity.getWindow().getDecorView().setSystemUiVisibility(pIsDark ? (lFlags & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) : (lFlags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR));
}
Egalitarian answered 27/4, 2017 at 15:31 Comment(1)
Working fine for me.Warfield
B
10

For Android 6.0 and above You can check Sunita's answer.

Android 5.0

we are able to change only status bar color not status bar text color.

there is no method like

setStatusBarTextColor(getResources().getColor(R.color.orange));

In short, it's not possible on Android 5.0. Check this answer

Biles answered 26/5, 2015 at 16:43 Comment(2)
It is possible from API 23 and above, check the answer below by SunitaHindorff
Lol, i just copypasted this code, even didn't read that there is no method like ...Snowblink
P
10

You can also do this at runtime. Here is an example for Mono.Android using the flag SystemUiVisibility. You have to do some bitwise operations to change the flag. Your application must be set to target API 23 or higher to compile with this flag.

//Android 6.0 introduced the ability to set a light colored text on the status bar
//MyActivity needs to be changed to your activity

if(Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
{
    int newUiVisibility = (int)MyActivity.Window.DecorView.SystemUiVisibility;

    if(state == StatusBarState.Light)
    {
        //Dark Text to show up on your light status bar
        newUiVisibility |= (int)Android.Views.SystemUiFlags.LightStatusBar;
    }
    else if(state == StatusBarState.Dark)
    {
        //Light Text to show up on your dark status bar
        newUiVisibility &= ~(int)Android.Views.SystemUiFlags.LightStatusBar;
    }

    MyActivity.Window.DecorView.SystemUiVisibility = (Android.Views.StatusBarVisibility)newUiVisibility;
}

public enum StatusBarState
{
    Light,
    Dark
}
Pompey answered 13/4, 2016 at 15:57 Comment(2)
What is Android object? Can't define itChavaree
@KonstantinKonopko It's a namespace within Xamarin. learn.microsoft.com/en-us/dotnet/api/…Pompey
M
7

FOR ANDROID 6.0+

You can use this to change the status bar content color to light on black background, as shown in below image.

View decorView = this.getWindow().getDecorView();
int systemUiVisibilityFlags = decorView.getSystemUiVisibility();
systemUiVisibilityFlags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
decorView.setSystemUiVisibility(systemUiVisibilityFlags);

enter image description here

By default, it is black so leave it for the light background.

Mcfall answered 30/3, 2019 at 15:47 Comment(0)
I
4

@Sunita answer is correct, but if you are using API 21 the status bar color will be light if you set so and status bar text color will also be light, and on 23+ it will work fine. On new apis with themes and night themes (dark mode) I would do it by adding the v23/styles.xml and set the status bar background and text color there:

<item name="android:statusBarColor">@color/lightColor</item>
<item name="android:windowLightStatusBar">true</item>

And in the night/styles.xml:

<item name="android:statusBarColor" tools:targetApi="l">@color/darkColor</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>

The default styles.xml wouldn't contain any of this code, or just this, but remember to not set it to light:

<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>

This way we are setting the light background (and text color) for status bar but only for devices with api 23+. On devices <23 background will not be changed, as I think this is something that we dont want knowing that the text color will stay white. The dark theme was added on API 29, so we don't have to be afraid of dark theme on api 21 ;)

The drawback of this however is that we are adding another file that we will need to remember to manage.

Irritated answered 1/2, 2021 at 9:29 Comment(0)
C
1

You can use this line to set a darker text color when you are using the white color for the status bar.

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Calvo answered 20/11, 2018 at 6:8 Comment(0)
S
1

My preferred solution until now

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {    
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this, R.color.colorSecondPrimaryColor));
}
Shilashilha answered 20/9, 2019 at 18:11 Comment(1)
SYSTEM_UI_FLAG_LIGHT_STATUS_BAR requires API 23 (Android M)Settlings

© 2022 - 2024 — McMap. All rights reserved.