Change status bar text color when primaryDark is white
Asked Answered
Z

17

138

I am trying to reproduce the behaviour of Google Calendar application: enter image description here

but I have not found a way to change the status text color. If i set the colorPrimaryDark as white I cannot see the icons neither text of status bar due their color is white as well.

Is there any way to change the status bar text color?

Zerlina answered 14/7, 2016 at 19:4 Comment(0)
C
271

I'm not sure what API level your trying to target, but if you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:

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

when android:windowLightStatusBar is set to true, status bar text color will be able to be seen when the status bar color is white, and vice-versa when android:windowLightStatusBar is set to false, status bar text color will be designed to be seen when the status bar color is dark.

Example:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- Status bar stuff. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">true</item> 
</style>
Confect answered 14/7, 2016 at 20:10 Comment(8)
if my api level is 21 it is not working can you tell me the alternative for thatNessy
Any solution found for Api 21. Please help?Amphiprostyle
Note: android:windowLightStatusBar requires API level 23 (current min is 21) more... (⌘F1)Damage
For api 21 this is not possible. In google applications it's a black bar rather than a white.Widener
See this post for API Level < 23: #49255377Dawndawna
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item> also fits good with this answer.Converter
@BastiVagabond It is not solution, it just suppresses the warning. It still won't work on API < 23Pallium
For API 21+ see example below with WindowInsetsControllerCompatCasas
A
39

you can do that programmatically like this answer

just add this

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Apeman answered 10/7, 2018 at 12:44 Comment(1)
setSystemUiVisibility is deprecatedFilipino
P
33

The compat version works on API 23+.

Here it is:

// deprecated
// WindowInsetsControllerCompat(window, view).isAppearanceLightStatusBars = Boolean
// also deprecated
// ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = Boolean
WindowCompat.getInsetsController(window, decorView)?.isAppearanceLightStatusBars = Boolean

You can get window directly from an Activity.

I like to add it to Window extension methods:

fun Window.setLightStatusBars(b: Boolean) {
    WindowCompat.getInsetsController(this, decorView)?.isAppearanceLightStatusBars = b
}

You need androidx.core for this

Pawn answered 14/2, 2021 at 15:44 Comment(2)
Ace -> Nice! Thank you!Fungiform
This method has no effect on API <23. developer.android.com/reference/androidx/core/view/…Kishakishinev
J
29

it's very simple:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this,R.color.white));// set status background white

and vice versa:

getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this, R.color.black));
View decorView = getWindow().getDecorView(); //set status background black 
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); //set status text  light
Job answered 3/6, 2019 at 19:2 Comment(0)
S
17

Following @Jon's answer I would update it a little but on new apis. 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.

Sruti answered 1/2, 2021 at 9:28 Comment(0)
O
4

As previous, the SYSTEM_UI_FLAG_LIGHT_STATUS_BAR do the work in my case, don't forget to set for higher than API 22.

add this to oncreate after the setContentView:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
Octahedrite answered 29/1, 2019 at 11:22 Comment(0)
C
4

With API 21+, this works for me:

WindowInsetsControllerCompat windowInsetsController =
                WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetsController.setAppearanceLightStatusBars(true);

I use

Theme.AppCompat.DayNight

and this code works for the Day and for the Night mode:

getWindow().setStatusBarColor(getWindow().getNavigationBarColor());
WindowInsetsControllerCompat windowInsetsController =
                WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO)
            windowInsetsController.setAppearanceLightStatusBars(true);
        else
            windowInsetsController.setAppearanceLightStatusBars(false);
Casas answered 17/8, 2022 at 11:47 Comment(1)
true answer is hereLithotomy
H
3
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark

getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimaryDark));// set status background white

It works for me

Hibbler answered 6/4, 2020 at 23:58 Comment(1)
This requires API level 23+.Boatbill
S
2

Try this once.

In your activity onCreate() method, paste the following code.

try {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_red));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Note: color_red - is the status bar colour.

Sempach answered 15/5, 2019 at 8:30 Comment(1)
The question is about the text color of the status bar, not the status bar background.Boatbill
U
2

In your activity onCreate() method, paste the following code after the setContentView(R.layout.activity_generic_main);

Here is the sample code below.

public class GenericMain extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generic_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

    }
}
Unbolt answered 15/4, 2020 at 16:11 Comment(0)
M
2

Try this if not splash page

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setNavigationBarColor(ContextCompat.getColor(context, R.color.white));
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.white));
Mariannamarianne answered 19/10, 2020 at 12:15 Comment(0)
P
2

To have a white status bar and black text color do this (Kotlin):

In the onCreate function of your main activity add this

val window: Window = window
WindowInsetsControllerCompat(window,window.decorView).isAppearanceLightStatusBars = true

In resoursces/styles.xml add this

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:statusBarColor">#ffffff</item> <!-- this line sets the status bar color (in my case #ffffff is white) -->
<!-- the following lines are not required -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

This works with API level 21 as well.

Propagandist answered 17/3, 2022 at 12:28 Comment(0)
L
1

COMPOSE:

I had a problem when changing theme from dark to light and vice versa. These lines of code worked for me:

val view = LocalView.current
val window = (view.context as Activity).window
window.statusBarColor = rememberedColors.surface.toArgb() //Some Color
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = isSystemInDarkTheme().not()
Lyte answered 4/11, 2023 at 6:58 Comment(0)
T
1

In case you are trying to change color of icons and texts in the status bar, to dark color so those get visible, you can do this, keep in mind this have to be done OnViewCreated of the Fragment

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        activity?.window?.let {
            WindowCompat.setDecorFitsSystemWindows(it, true)
            WindowInsetsControllerCompat(it, view).isAppearanceLightStatusBars = true
        }
    } else { // deprecated method for older versions
        var flags: Int = view.systemUiVisibility
        flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        view.systemUiVisibility = flags
    }
Terrenceterrene answered 24/11, 2023 at 5:24 Comment(0)
S
0

For anyone in the future looking to change status bar color from white programmatically in a fragment and back to primary dark when leaving fragment for minimum api 21< 23 in android using Java

private void updateStatusBar(boolean isEnter){

    Window window = requireActivity().getWindow();

    int color = ContextCompat.getColor(requireActivity(),R.color.colorAlertDialog);

    if(isEnter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        else
            clearDecorFlags(window);
    }
    else {
        color = ContextCompat.getColor(requireActivity(),R.color.colorPrimaryDark);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        else
            clearDecorFlags(window);
    }
    window.setStatusBarColor(color);
}

private void clearDecorFlags(Window window){
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
Schock answered 3/2, 2021 at 14:32 Comment(1)
Override text color?Schock
Q
0

So it's a bit different in case of kotlin

//for Dark status bar icon with white background

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
            getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.white))    
    
             
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv())
            getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.black))

 // for dark background and light theme colours of icon.
Quarto answered 23/4, 2021 at 4:19 Comment(0)
S
0

For future readers

If you have enabled edgeToEdge then you can use this.

For dark content color

enableEdgeToEdge(
        statusBarStyle = SystemBarStyle.light(android.graphics.Color.TRANSPARENT, android.graphics.Color.TRANSPARENT)
    )

You might be facing this because your device has dark mode enabled and you are drawing your custom background in status bar, this happens because by default enableEdgeToEdge uses SystemBarStyle.auto for statusBarStyle.

Don't forget to put enableEdgeToEdge before super.onCreate() of the activity.

Sacellum answered 15/3 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.