Search code examples
androidkotlinandroid-jetpack-compose

enableEdgeToEdge style not working in andorid 15 [AppCompatActivity]


I'm trying to change the status bar and navigation bar colors using enableEdgeToEdge() in Android 15 (API 34), but the colors are not updating.

Here’s my code inside onCreate() in my MainActivity:



class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Force Light Mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Convert Int to Color
        val primaryDarkColor = Color.RED

        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            ),
            navigationBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            )
        )

        // Adjust window insets
        ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(0, systemBars.top, 0, 0)
            insets
        }

       
    }

 
}



Solution

  • According to the official documentation:

    If the app targets VANILLA_ICE_CREAM or above, the color will be transparent and cannot be changed.

    This means that if your app targets android 15 or higher, the system bars (status bar and navigation bar) will automatically match the background color of your view since edge-to-edge is enforced.

    If you only want to change the color of the status bar, you could create a spacer view with full width and no height. Apply WindowInsets as padding to match the status bar height.

    please refer to:

    https://developer.android.com/reference/android/view/Window#setStatusBarColor(int)

    https://developer.android.com/develop/ui/views/layout/edge-to-edge#system_bar_protection