Skip to content

Commit a3ee09e

Browse files
authored
Merge pull request #817 from android/av/enable-edge-to-edge
Switch to new Activity.enableEdgeToEdge
2 parents 2693773 + 2b218b9 commit a3ee09e

File tree

7 files changed

+36
-71
lines changed

7 files changed

+36
-71
lines changed

app/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ dependencies {
106106
debugImplementation(libs.androidx.compose.ui.testManifest)
107107
debugImplementation(project(":ui-test-hilt-manifest"))
108108

109-
implementation(libs.accompanist.systemuicontroller)
110109
implementation(libs.androidx.activity.compose)
111110
implementation(libs.androidx.appcompat)
112111
implementation(libs.androidx.core.ktx)

app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package com.google.samples.apps.nowinandroid
1919
import android.os.Bundle
2020
import android.util.Log
2121
import androidx.activity.ComponentActivity
22+
import androidx.activity.SystemBarStyle
2223
import androidx.activity.compose.setContent
24+
import androidx.activity.enableEdgeToEdge
2325
import androidx.activity.viewModels
2426
import androidx.compose.foundation.isSystemInDarkTheme
2527
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
@@ -31,13 +33,11 @@ import androidx.compose.runtime.getValue
3133
import androidx.compose.runtime.mutableStateOf
3234
import androidx.compose.runtime.setValue
3335
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
34-
import androidx.core.view.WindowCompat
3536
import androidx.lifecycle.Lifecycle
3637
import androidx.lifecycle.lifecycleScope
3738
import androidx.lifecycle.repeatOnLifecycle
3839
import androidx.metrics.performance.JankStats
3940
import androidx.profileinstaller.ProfileVerifier
40-
import com.google.accompanist.systemuicontroller.rememberSystemUiController
4141
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
4242
import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
4343
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper
@@ -108,16 +108,28 @@ class MainActivity : ComponentActivity() {
108108
}
109109

110110
// Turn off the decor fitting system windows, which allows us to handle insets,
111-
// including IME animations
112-
WindowCompat.setDecorFitsSystemWindows(window, false)
111+
// including IME animations, and go edge-to-edge
112+
// This also sets up the initial system bar style based on the platform theme
113+
enableEdgeToEdge()
113114

114115
setContent {
115-
val systemUiController = rememberSystemUiController()
116116
val darkTheme = shouldUseDarkTheme(uiState)
117117

118-
// Update the dark content of the system bars to match the theme
119-
DisposableEffect(systemUiController, darkTheme) {
120-
systemUiController.systemBarsDarkContentEnabled = !darkTheme
118+
// Update the edge to edge configuration to match the theme
119+
// This is the same parameters as the default enableEdgeToEdge call, but we manually
120+
// resolve whether or not to show dark theme using uiState, since it can be different
121+
// than the configuration's dark theme value based on the user preference.
122+
DisposableEffect(darkTheme) {
123+
enableEdgeToEdge(
124+
statusBarStyle = SystemBarStyle.auto(
125+
android.graphics.Color.TRANSPARENT,
126+
android.graphics.Color.TRANSPARENT,
127+
) { darkTheme },
128+
navigationBarStyle = SystemBarStyle.auto(
129+
lightScrim,
130+
darkScrim,
131+
) { darkTheme },
132+
)
121133
onDispose {}
122134
}
123135

@@ -224,3 +236,15 @@ private fun shouldUseDarkTheme(
224236
DarkThemeConfig.DARK -> true
225237
}
226238
}
239+
240+
/**
241+
* The default light scrim, as defined by androidx and the platform:
242+
* https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt;l=35-38;drc=27e7d52e8604a080133e8b842db10c89b4482598
243+
*/
244+
private val lightScrim = android.graphics.Color.argb(0xe6, 0xFF, 0xFF, 0xFF)
245+
246+
/**
247+
* The default dark scrim, as defined by androidx and the platform:
248+
* https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt;l=40-44;drc=27e7d52e8604a080133e8b842db10c89b4482598
249+
*/
250+
private val darkScrim = android.graphics.Color.argb(0x80, 0x1b, 0x1b, 0x1b)

app/src/main/res/values-night/themes.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
-->
1717
<resources xmlns:tools="http://schemas.android.com/tools">
1818

19-
<style name="NightAdjusted.Theme.Nia" parent="android:Theme.Material.NoActionBar">
20-
<item name="android:windowLightStatusBar" tools:targetApi="23">false</item>
21-
<item name="android:windowLightNavigationBar" tools:targetApi="27">false</item>
22-
</style>
19+
<style name="NightAdjusted.Theme.Nia" parent="android:Theme.Material.NoActionBar" />
2320

2421
<style name="NightAdjusted.Theme.Splash" parent="Theme.SplashScreen">
2522
<item name="android:windowLightStatusBar" tools:targetApi="23">false</item>

app/src/main/res/values-v23/themes.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/src/main/res/values-v27/themes.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/src/main/res/values/themes.xml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,10 @@
1818

1919
<!-- Allows us to override night specific attributes in the
2020
values-night folder. -->
21-
<style name="NightAdjusted.Theme.Nia" parent="android:Theme.Material.Light.NoActionBar">
22-
<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
23-
<item name="android:windowLightNavigationBar" tools:targetApi="27">true</item>
24-
</style>
25-
26-
<!-- Allows us to override platform level specific attributes in their
27-
respective values-vXX folder. -->
28-
<style name="PlatformAdjusted.Theme.Nia" parent="NightAdjusted.Theme.Nia">
29-
<item name="android:statusBarColor">@color/black30</item>
30-
</style>
21+
<style name="NightAdjusted.Theme.Nia" parent="android:Theme.Material.Light.NoActionBar" />
3122

3223
<!-- The final theme we use -->
33-
<style name="Theme.Nia" parent="PlatformAdjusted.Theme.Nia" />
24+
<style name="Theme.Nia" parent="NightAdjusted.Theme.Nia" />
3425

3526
<style name="NightAdjusted.Theme.Splash" parent="Theme.SplashScreen">
3627
<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>

gradle/libs.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
accompanist = "0.28.0"
33
androidDesugarJdkLibs = "2.0.3"
44
androidGradlePlugin = "8.1.0"
5-
androidxActivity = "1.7.0"
5+
androidxActivity = "1.8.0-alpha06"
66
androidxAppCompat = "1.5.1"
77
androidxBrowser = "1.4.0"
88
androidxComposeBom = "2023.06.01"
@@ -55,7 +55,6 @@ turbine = "0.12.1"
5555

5656
[libraries]
5757
accompanist-permissions = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanist" }
58-
accompanist-systemuicontroller = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" }
5958
accompanist-testharness = { group = "com.google.accompanist", name = "accompanist-testharness", version.ref = "accompanist" }
6059
android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }
6160
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }

0 commit comments

Comments
 (0)