After updating my SDK to version 35, I noticed that my app’s UI now starts directly from the top of the display, including behind the status bar. Previously, the design started below the status bar. To understand how edge-to-edge functionality works, I created a sample layout.
The XML layout I am using is as follows:
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
android:id="@+id/main">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
With android:fitsSystemWindows="true"
, my output looks like this:
In this case, the status bar and AppBarLayout
share the same color.
However, when I change android:fitsSystemWindows
to "false"
and use the following code in my activity:
ViewCompat.setOnApplyWindowInsetsListener(
findViewById<View>(R.id.main)
) { v: View, insets: WindowInsetsCompat ->
val systemBars: Insets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
I get a different result:
Now the status bar and AppBarLayout
have different colors.
This confuses me. When I set android:fitsSystemWindows="true"
in XML, the status bar and AppBarLayout
are the same color. But when I handle the insets programmatically, the colors are different. I tried setting the status bar color with this.getWindow().setStatusBarColor
, but it no longer works from Android 15 (SDK 35). I also tried using the following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.setSystemBarsAppearance(
APPEARANCE_LIGHT_STATUS_BARS, // For light status bar icons
APPEARANCE_LIGHT_STATUS_BARS
)
} else {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR // For light status bar icons
}
But the status bar color still doesn’t change.
Questions:
- Why is edge-to-edge functionality mandatory?
- How do I properly implement edge-to-edge in a regular Android app (non-gaming)?
- What are the potential side effects of using edge-to-edge, such as issues with height calculations?
- Need solution for my issue.
I would appreciate any help on this.
Why is edge-to-edge functionality mandatory?
Yes, Android 15 enforces edge-to-edge Before target SDK 35 (Android 15), your app does not draw edge-to-edge without explicit code changes to intentionally go edge-to-edge. After setting targetSdk=35 or higher, the system will draw your app edge-to-edge by default on Android 15 and later devices.
How do I properly implement edge-to-edge in a regular Android app (non-gaming)?
For apps targeting Android 15 : By default, your app will draw behind system bars, enabling edge-to-edge. You may need to handle insets as described below.
For apps targeting older Android versions:
- You’ll need to manually opt-in:
WindowCompat.setDecorFitsSystemWindows(window, false)
in your Activity’s onCreate() method. This makes the window layout handle system windows (status bar, navigation bar) instead of your content.
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
// Apply padding or margins based on systemBarsInsets
v.updatePadding(top = systemBarsInsets.top,
bottom = systemBarsInsets.bottom,
left = systemBarsInsets.left,
right = systemBarsInsets.right)
insets // or insets.consumeSystemWindowInsets()
}
If you are using Material Design Components, they often have built-in support for edge-to-edge:
- AppBarLayout: Use
app:layout_insetEdge="top"
and the appropriate
fitsSystemWindows
settings to ensure it extends behind the status
bar. - DrawerLayout: Similar to AppBarLayout, adjust its layout behavior.
- BottomNavigationView: Use
app:layout_insetEdge="bottom"
and adjust
margins or padding to avoid overlapping the navigation bar.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
WindowCompat.setDecorFitsSystemWindows(window, false)
// Example: Applying insets to a content view
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.my_content_view)) { v, insets ->
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.updatePadding(top = systemBarsInsets.top, bottom = systemBarsInsets.bottom)
insets
}
}
}
1