Skip to content

Instantly share code, notes, and snippets.

@limkhashing
Created February 12, 2025 14:40
Show Gist options
  • Save limkhashing/31138146cea96bc20c3d66a6628ce49d to your computer and use it in GitHub Desktop.
Save limkhashing/31138146cea96bc20c3d66a6628ce49d to your computer and use it in GitHub Desktop.
Handle Edge to Edge that was enforced on Android 15
@Composable
fun ScreenContent() {
EdgeToEdgeDemoTheme {
// Need to add Scaffold, and can apply Material 3 theme. Scaffold will automatically handle the window insets
Scaffold(
topBar = {
TopAppBar(
title = { Text("Hello world!") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = Color(0xFF37B362)
)
)
},
bottomBar = {
BottomAppBar(
containerColor = Color(0xFF37B362)
) {
}
},
contentWindowInsets = WindowInsets.safeDrawing,
containerColor = Color(0xFFD53C00)
) { innerPadding ->
LazyColumn(
contentPadding = innerPadding,
) {
items(100) {
Text(
text = "Item $it",
modifier = Modifier
.fillMaxWidth()
.background(Color.White)
.padding(16.dp)
)
}
}
}
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.my_text_view)
ViewCompat.setOnApplyWindowInsetsListener(textView) { view, insets ->
val innerPadding = insets.getInsets(
WindowInsetsCompat.Type.systemBars() or
WindowInsetsCompat.Type.displayCutout()
)
textView.setPadding(
innerPadding.left,
innerPadding.top,
innerPadding.right,
innerPadding.bottom
)
insets
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment