Jetpack Compose - Box Layout

How to use box layout in android jetpack compose.

The following code snippet shows Child elements of a box layout can have 9 different alignments.

Alignments of Child Elements of a Box.


package com.compose.example

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
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.compose.example.ui.theme.ComposeExampleTheme

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeExampleTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainContent()
}
}
}
}
}



@Composable
fun MainContent() {

Box(
modifier = Modifier
.background(color = Color.LightGray)
.fillMaxSize()
) {

Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.TopStart),
text = "TopStart"
)
Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.TopCenter),
text = "TopCenter"
)
Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.TopEnd),
text = "TopEnd"
)

Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.CenterStart),
text = "CenterStart"
)

Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.Center),
text = "Center"
)
Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.CenterEnd),
text = "CenterEnd"
)

Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.BottomStart),
text = "BottomStart"
)
Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.BottomCenter),
text = "BottomCenter"
)
Text(
style = MaterialTheme.typography.labelLarge,
modifier = Modifier
.background(Color.White)
.padding(10.dp)
.align(Alignment.BottomEnd),
text = "BottomEnd"
)
}
}

..

Comments