HomeAndroidFundamentals of Compose layouts and modifiers | by Simona Stojanovic | Android...

Fundamentals of Compose layouts and modifiers | by Simona Stojanovic | Android Builders | Feb, 2023


Greetings, people
/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAlien(
coloration: Shade,
modifier: Modifier = Modifier,
) {
Picture(
modifier = modifier,
painter = painterResource(R.drawable.android_alien),
colorFilter = ColorFilter.tint(coloration = coloration),
contentDescription = null
)
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliens() {
AndroidAlien(
coloration = Shade.Pink,
modifier = Modifier
.dimension(70.dp)
.padding(4.dp)
)
AndroidAlien(
coloration = Shade.Inexperienced,
modifier = Modifier
.dimension(70.dp)
.padding(4.dp)
)
}

A single, lonely Android Alien
/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliens() {
AndroidAlien(
coloration = Shade.Pink,
modifier = Modifier.dimension(100.dp)
)
// …
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row {
AndroidAlien(…)
AndroidAlien(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensColumn() {
Column {
AlienShip(...)
AlienShip(...)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row(
horizontalArrangement = Association.Middle,
verticalAlignment = Alignment.Prime,
) {
AndroidAlien(…)
AndroidAlien(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensColumn() {
Column(
verticalArrangement = Association.Prime,
horizontalAlignment = Alignment.CenterHorizontally,
) {
AndroidAlien(…)
AndroidAlien(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = Association.Middle,
verticalAlignment = Alignment.Prime,
) {
AndroidAlien(…)
AndroidAlien(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensColumn() {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Association.Prime,
horizontalAlignment = Alignment.CenterHorizontally,
) {
AndroidAlien(…)
AndroidAlien(…)
}
}

Insurgent yell
/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row(…) {
AndroidAlien(…)
AndroidAlien(…)
AndroidAlien(…)
AndroidAlien(
coloration = Shade.Pink,
modifier = Modifier.align(Association.CenterVertically)
) // Rogue Revolt ship
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row(…) {
AndroidAlien(modifier = Modifier.dimension(70.dp)) // Takes precisely 70 DP
AndroidAlien(modifier = Modifier.weight(1F)) // Will get the remaining
AndroidAlien(modifier = Modifier.dimension(70.dp)) // Takes precisely 70 DP
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensRow() {
Row(…) {
AndroidAlien(modifier = Modifier.weight(1F))
AndroidAlien(modifier = Modifier.weight(2F))
AndroidAlien(modifier = Modifier.weight(1F))
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

AndroidAlien(
modifier = Modifier
.dimension(70.dp)
.weight(1F, fill = false)
)

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensGameOverBox() {
Field {
AndroidAliensRow(…)
Textual content(
textual content = “GAME OVER”
// …
)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensGameOverBox() {
Field(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Middle
) {
AndroidAliensRow(…)
Textual content(
textual content = “GAME OVER”
// …
)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensGameOverBox() {
Field(…) {
AndroidAliensRow(…)
Spacer(
modifier = Modifier
.matchParentSize()
.background(coloration = Shade.Grey.copy(alpha = .7f))
)
Textual content(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensWithInfo() {
Scaffold(
topBar = {
InfoHeader(…)
},
bottomBar = {
Button(…) {
Textual content(
textual content = “PRESS START”
)
}
}
) {
AndroidAliens(…)
}
}

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable Scaffold(
// …
topBar: @Composable () -> Unit = {},
bottomBar: @Composable () -> Unit = {},
// …
)

/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensWithInfo() {
Scaffold(
topBar = {
ComposeShipsRow(…)
},
bottomBar = {
AndroidAliensRow(…)
}
) {
AndroidAliens(…)
}
}

AlienShipStackOverflow
/* Copyright 2023 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

@Composable
enjoyable AndroidAliensGrid() {
// Specify that the grid ought to have 5 columns exactly
LazyVerticalGrid(columns = GridCells.Fastened(5)) {
// Add 50 gadgets or inexperienced Android aliens
gadgets(50) {
AndroidAlien(…)
}
}
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments