5

I want to set different colors for dark theme and light theme. Everything works fine except status bar color.

In dark theme -> my status bar color not updated.

My theme :

private val DarkColorPalette = darkColors(
    primary = Color.Black,
    primaryVariant = Color.Black,
    secondary = Color.LightGray
)

private val LightColorPalette = lightColors(
    primary = Purple500,
    primaryVariant = Purple700,
    secondary = Teal200
)

My output in Dark theme:

enter image description here

Edit:

I already saw this answer - how to change statusbar color in jetpack compose?

They recommend to use the library. I don't want to use any 3rd party library.

Edit 2:

my manifest file

   <activity
        android:name=".ActivityTextStyles"
        android:exported="true"
        android:label="@string/app_name"
        android:theme="@style/Theme.ComposeText.NoActionBar" />
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

3 Answers3

8

Here's what I discovered when I faced this same issue and decided to look deeper for the cause.

Turns out, out of the box, my Compose project reads the value for status bar color from the themes.xml file in res/values. The value is set using colorPrimaryVariant. Updating that value to a new color changed the color of the status bar.

Overall, I will recommend updating the values of themes.xml and colors.xml to the custom colors you wish to use for your project.

Eaweb
  • 811
  • 1
  • 12
  • 16
6

I was able to set status bar color with this one line code in onCreate()

  this.window.statusBarColor = ContextCompat.getColor(this,R.color.black)

My code:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      
            this.window.statusBarColor = ContextCompat.getColor(this,R.color.black)
            //your composables
         }
     }
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
2

In my case, the color of the top bar was the only one overwritten in the themes.xml file generated when creating the project in Android Studio.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.MyApp" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:statusBarColor">@color/purple_700</item>
    </style>
</resources>

It can be modified there.

miel3k
  • 211
  • 3
  • 3