androidwebviewandroid-15edge-to-edge

How to layout a webview for Android 15 edge to edge


I have an app consisting only of a webview. I'm trying it to update for Android 15 with edge to edge.

Below the current code from layout/webview.xml and MainActivity.kt. It worked well on Android 14.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        actionBar?.hide()
        super.onCreate(savedInstanceState)
        setContentView(layout.webview)
/* code from Empty Views Activity */
        //enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

I added the code for edge to edge form an empty project Empty Views Activity generated with New... - New Project....

Now on Android 15, the part of the app extending under the status bar is always white, even if the device has night mode on. The example code seems to work if the ConstraintLayout contains a Textview, but not if it contains a WebView.

System bar in night mode

Bottom bar in night mode How can we fix this?


Solution

  • now you need to set android:background for ContraintLayout. if you want to support dark mode then reference to color stored in resources

    android:background="@color/status_bar_color"
    

    and set different status_bar_color separately in values/colors.xml and values-night/colors.xml. or you can detect night mode and simply use

    val color = if (dark) Color.BLACK else Color.WHITE
    findViewById(id.main).setBackgroundColor(color)
    

    I'm also advising to inspect your style set for this Activity, as wrongly configured may lead to white icons on white status bar (or black on black) - look for proper windowLightStatusBar, windowDrawsSystemBarBackgrounds and statusBarColor (which won't work when you target 35+ and won't opt-out with windowOptOutEdgeToEdgeEnforcement)