javaandroid

How to change the status bar color?


I'm facing an issue with XML layout in Android Studio while developing an application. I'm unable to change the color of the status bar, as shown in the image, to white.

Screenshot

How can I resolve this problem? I searched for solutions online, but couldn't find anything suitable for my specific project.


Solution

  • Okay, Your status bar is not changing color or still shows a colored background (like in the screenshot), you need to update both your theme and Activity code to handle it correctly.

    1. In your MainActivity.kt (or the Activity where you want the status bar to be white), add this code in the onCreate() method:
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.statusBarColor = Color.WHITE
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }
    
    1. Update Your Theme in themes.xml
      In res/values/themes.xml, modify your theme like this:
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Set status bar color to white -->
        <item name="android:statusBarColor">@android:color/white</item>
    
        <!-- Ensures dark icons on white background -->
        <item name="android:windowLightStatusBar">true</item>
    </style>
    

    Then make sure your AndroidManifest.xml points to this theme:

    <application
        android:theme="@style/AppTheme">
        ...
    </application>
    

    3. Ensure No Theme Override in Layout XML

    Sometimes, the AppBar or Toolbar can override the status bar theme. In your layout XML:

    <com.google.android.material.appbar.AppBarLayout
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"  <!-- ❌ Avoid this -->
        ... />
    

    4. Use fitsSystemWindows in Root Layout (Optional)

    <LinearLayout
        android:fitsSystemWindows="true"
        ... >
        ...
    </LinearLayout>
    

    hope it helps!