Last active
September 25, 2023 13:05
-
-
Save benigumocom/5d1044a38012aff5a14553f3481b03fb to your computer and use it in GitHub Desktop.
Android Developers Japan Blog: 古い端末でも Edge-to-edge にしたい 👉 https://android-developers-jp.googleblog.com/2023/01/is-your-app-providing-a-backward-compatible-edge-to-edge-experience.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.e2e.ui | |
import android.content.res.Configuration | |
import android.content.res.Resources | |
import android.os.Build | |
import android.view.View | |
import android.view.Window | |
import android.view.WindowManager | |
import androidx.annotation.RequiresApi | |
import androidx.core.app.ComponentActivity | |
import androidx.core.content.res.ResourcesCompat | |
import androidx.core.view.WindowCompat | |
import androidx.core.view.WindowInsetsControllerCompat | |
import com.example.android.e2e.R | |
/** | |
* Configures edge-to-edge for the activity. | |
* | |
* ``` | |
* override fun onCreate(savedInstanceState: Bundle?) { | |
* setUpEdgeToEdge() | |
* super.onCreate(savedInstanceState) | |
* ... | |
* } | |
* ``` | |
*/ | |
fun ComponentActivity.setUpEdgeToEdge() { | |
val impl = if (Build.VERSION.SDK_INT >= 29) { | |
EdgeToEdgeApi29() | |
} else if (Build.VERSION.SDK_INT >= 26) { | |
EdgeToEdgeApi26() | |
} else if (Build.VERSION.SDK_INT >= 23) { | |
EdgeToEdgeApi23() | |
} else if (Build.VERSION.SDK_INT >= 21) { | |
EdgeToEdgeApi21() | |
} else { | |
EdgeToEdgeBase() | |
} | |
impl.setUp(window, findViewById(android.R.id.content), theme) | |
} | |
private fun isDarkMode(resources: Resources): Boolean { | |
return (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES | |
} | |
private interface EdgeToEdgeImpl { | |
fun setUp(window: Window, view: View, theme: Resources.Theme) | |
} | |
@RequiresApi(29) | |
private class EdgeToEdgeApi29 : EdgeToEdgeImpl { | |
override fun setUp(window: Window, view: View, theme: Resources.Theme) { | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
val resources = view.resources | |
val transparent = ResourcesCompat.getColor(resources, android.R.color.transparent, theme) | |
val isDarkMode = isDarkMode(resources) | |
window.statusBarColor = transparent | |
window.navigationBarColor = transparent | |
val controller = WindowInsetsControllerCompat(window, view) | |
controller.isAppearanceLightStatusBars = !isDarkMode | |
controller.isAppearanceLightNavigationBars = !isDarkMode | |
} | |
} | |
@RequiresApi(26) | |
private class EdgeToEdgeApi26 : EdgeToEdgeImpl { | |
override fun setUp(window: Window, view: View, theme: Resources.Theme) { | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
val resources = view.resources | |
val transparent = ResourcesCompat.getColor(resources, android.R.color.transparent, theme) | |
// R.color.navigation_bar_scrim_light is #63FFFFFF for example. | |
val scrim = ResourcesCompat.getColor(resources, R.color.navigation_bar_scrim_light, theme) | |
window.statusBarColor = transparent | |
window.navigationBarColor = scrim | |
val controller = WindowInsetsControllerCompat(window, view) | |
controller.isAppearanceLightStatusBars = true | |
controller.isAppearanceLightNavigationBars = true | |
} | |
} | |
@RequiresApi(23) | |
private class EdgeToEdgeApi23 : EdgeToEdgeImpl { | |
override fun setUp(window: Window, view: View, theme: Resources.Theme) { | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
val resources = view.resources | |
val transparent = ResourcesCompat.getColor(resources, android.R.color.transparent, theme) | |
window.statusBarColor = transparent | |
val controller = WindowInsetsControllerCompat(window, view) | |
controller.isAppearanceLightStatusBars = true | |
@Suppress("DEPRECATION") | |
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) | |
} | |
} | |
@RequiresApi(21) | |
private class EdgeToEdgeApi21 : EdgeToEdgeImpl { | |
override fun setUp(window: Window, view: View, theme: Resources.Theme) { | |
WindowCompat.setDecorFitsSystemWindows(window, false) | |
@Suppress("DEPRECATION") | |
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) | |
@Suppress("DEPRECATION") | |
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) | |
} | |
} | |
private class EdgeToEdgeBase : EdgeToEdgeImpl { | |
override fun setUp(window: Window, view: View, theme: Resources.Theme) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EdgeToEdge.kt - Android Code Search
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt
Note: This version will only compile against the Android 14 (Upside Down Cake) Beta 1 SDK or higher.
https://developer.android.com/jetpack/androidx/releases/activity#1.8.0-alpha03