Created
January 24, 2026 17:35
-
-
Save rubywai/3839c759f4fbcb521f029ac2269f2cd0 to your computer and use it in GitHub Desktop.
@12 grid layout in compose
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.rubylearner.composeapp | |
| import android.os.Bundle | |
| import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.setContent | |
| import androidx.activity.enableEdgeToEdge | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.layout.Arrangement | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.height | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid | |
| import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells | |
| import androidx.compose.material3.ExperimentalMaterial3Api | |
| import androidx.compose.material3.Scaffold | |
| import androidx.compose.material3.Text | |
| import androidx.compose.material3.TopAppBar | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.unit.dp | |
| import com.rubylearner.composeapp.ui.theme.ComposeAppTheme | |
| class MainActivity : ComponentActivity() { | |
| @OptIn(ExperimentalMaterial3Api::class) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| setContent { | |
| ComposeAppTheme { | |
| Scaffold( | |
| modifier = Modifier.fillMaxSize(), | |
| topBar = { | |
| TopAppBar( | |
| title = { | |
| Text("Grid View Lesson") | |
| } | |
| ) | |
| } | |
| ) { innerPadding -> | |
| MyGrid( | |
| modifier = Modifier.padding(innerPadding) | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| @Composable | |
| fun MyGrid(modifier: Modifier) { | |
| LazyVerticalStaggeredGrid ( | |
| columns = StaggeredGridCells.Adaptive(150.dp), | |
| modifier = modifier, | |
| horizontalArrangement = Arrangement.spacedBy(4.dp), | |
| verticalItemSpacing = 4.dp, | |
| reverseLayout = true | |
| ) { | |
| items(200) { | |
| Box( | |
| modifier = Modifier | |
| .height(if(it %2 == 0) 200.dp else 150.dp) | |
| .background(Color.DarkGray) | |
| ) { | |
| Text( | |
| "Item $it", | |
| modifier = Modifier.align(Alignment.Center), | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment