Compose Card Content Alignment
The Card is a widget of the jetpack compose library. A Card layout displays a single widget inside it. The Card is useful to render a specified shape with elevation. The Card also can draw a background color on its surface. The jetpack compose developer can put a border/stroke around the Card layout itself. But how can they align the content inside of a Card layout?
By default, the Card widget constructor has no argument to directly set alignment for its child element. So we have to write extra code to define the alignment of a Card child. The following jetpack compose tutorial will demonstrate to us how we can set a Card content alignment in an android application.
The Box is another layout widget of the android jetpack compose library. We can set alignment for the Box content using its constructor’s ‘contentAlignment’ argument.
The Card widget tries to expand its child size to fill the parent Card size. To align the Card content, we have to wrap the Card child element with a Box widget to define its position inside the Card layout. The alignment of the Box child element will act as the Card content alignment.
The default value of the Box ‘contentAlignment’ argument is ‘Alignment.TopStart’. There are many available values for the ‘contentAlignment’ argument such as Alignment.Center, Alignment.TopEnd, Alignment.BottomStart, Alignment.CenterStart, etc. Those names are self-describing. So, if we want to put an element at the exact center of the Box layout (practically Card layout), then we can simply pass the ‘contentAlignment’ argument value to ‘Alignment.Center’.
This jetpack compose tutorial code written in an android studio IDE. Copy the code and run it on an emulator device or a real device to test how we define the Card layout content alignment. We also displayed a screenshot of this tutorial’s emulator screen at the bottom of this tutorial.
By default, the Card widget constructor has no argument to directly set alignment for its child element. So we have to write extra code to define the alignment of a Card child. The following jetpack compose tutorial will demonstrate to us how we can set a Card content alignment in an android application.
The Box is another layout widget of the android jetpack compose library. We can set alignment for the Box content using its constructor’s ‘contentAlignment’ argument.
The Card widget tries to expand its child size to fill the parent Card size. To align the Card content, we have to wrap the Card child element with a Box widget to define its position inside the Card layout. The alignment of the Box child element will act as the Card content alignment.
The default value of the Box ‘contentAlignment’ argument is ‘Alignment.TopStart’. There are many available values for the ‘contentAlignment’ argument such as Alignment.Center, Alignment.TopEnd, Alignment.BottomStart, Alignment.CenterStart, etc. Those names are self-describing. So, if we want to put an element at the exact center of the Box layout (practically Card layout), then we can simply pass the ‘contentAlignment’ argument value to ‘Alignment.Center’.
This jetpack compose tutorial code written in an android studio IDE. Copy the code and run it on an emulator device or a real device to test how we define the Card layout content alignment. We also displayed a screenshot of this tutorial’s emulator screen at the bottom of this tutorial.
MainActivity.kt
package com.cfsuman.jetpackcompose
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GetScaffold()
}
}
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun GetScaffold(){
Scaffold(
topBar = {TopAppBar(
title = {Text(
"Compose - Card Alignment",
color = Color.White)},
backgroundColor = Color(0xFF58427C)) },
content = {MainContent()},
backgroundColor = Color(0xFFEDEAE0)
)
}
@Composable
fun MainContent(){
Column(
modifier = Modifier
.fillMaxSize()
.padding(12.dp),
verticalArrangement = Arrangement.SpaceEvenly,
horizontalAlignment = Alignment.CenterHorizontally
) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(150.dp),
backgroundColor = Color(0xFFE95C4B),
elevation = 4.dp,
shape = RoundedCornerShape(24.dp),
) {
Box(contentAlignment = Alignment.Center) {
Text(
text = "Card Alignment Center",
style = MaterialTheme.typography.h6
)
}
}
Card(
modifier = Modifier
.fillMaxWidth()
.height(150.dp),
backgroundColor = Color(0xFFE95C4B),
elevation = 4.dp,
shape = RoundedCornerShape(24.dp),
) {
Box(
Modifier.padding(12.dp),
contentAlignment = Alignment.BottomStart
) {
Text(
text = "Card Alignment Bottom",
style = MaterialTheme.typography.h6
)
}
}
Card(
modifier = Modifier
.fillMaxWidth()
.height(150.dp),
backgroundColor = Color(0xFFE95C4B),
elevation = 4.dp,
shape = RoundedCornerShape(24.dp),
) {
Box(
Modifier.padding(12.dp),
contentAlignment = Alignment.TopEnd
) {
Text(
text = "Card Alignment End",
style = MaterialTheme.typography.h6
)
}
}
}
}
@Preview
@Composable
fun ComposablePreview(){
//GetScaffold()
}
}
- jetpack compose - Card padding
- jetpack compose - Card background color
- jetpack compose - Card clickable
- jetpack compose - ModalDrawer example
- jetpack compose - Navigation multiple arguments
- jetpack compose - Navigation arguments data type
- jetpack compose - Navigation object argument
- jetpack compose - WebView ProgressIndicator
- jetpack compose - WebView progress percentage
- jetpack compose - Backdrop scaffold
- jetpack compose - Double click listener
- jetpack compose - Long click listener
- jetpack compose - Pass onClick event to function
- jetpack compose - Get primary language
- jetpack compose - Get screen orientation