This program shows a card with some padding, rounded corners, and shadow. Inside, it has a title and a description stacked vertically with space between.
Android Kotlin
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.tooling.preview.Preview
@Composable
fun SimpleCard() {
Card(
modifier = Modifier.padding(16.dp),
elevation = CardDefaults.cardElevation(6.dp),
shape = RoundedCornerShape(10.dp)
) {
Column(modifier = Modifier.padding(16.dp)) {
Text("Welcome to Card", style = MaterialTheme.typography.titleLarge)
Spacer(modifier = Modifier.height(8.dp))
Text("This is a simple card example in Jetpack Compose.")
}
}
}
@Preview(showBackground = true)
@Composable
fun PreviewSimpleCard() {
SimpleCard()
}
OutputSuccess
Important Notes
Cards automatically handle shadows and clipping for a neat look.
Use Modifier.padding() inside the card to space content away from edges.
Cards can be clickable by adding Modifier.clickable { }.
Summary
Cards group related content visually with padding, shadow, and shape.
Use Card composable with modifiers to customize appearance.
Cards improve UI clarity and can be interactive.
Practice
(1/5)
1. What is the primary purpose of using a Card composable in Android Jetpack Compose?
easy
A. To group related content visually with padding, shadow, and shape
B. To create a button with click actions
C. To display a list of items vertically
D. To handle navigation between screens
Solution
Step 1: Understand the role of Card composable
The Card composable is designed to group related UI elements visually by adding padding, shadows, and rounded corners.
Step 2: Differentiate from other composables
Buttons handle clicks, lists display multiple items, and navigation manages screen changes, which are not the primary roles of Card.
Final Answer:
To group related content visually with padding, shadow, and shape -> Option A
Quick Check:
Card groups content visually = A [OK]
Hint: Cards group content visually with style and padding [OK]
Common Mistakes:
Confusing Card with Button composable
Thinking Card manages navigation
Assuming Card is for lists only
2. Which of the following is the correct syntax to create a simple Card with padding in Jetpack Compose?
easy
A. Card(modifier = Modifier.padding(16.dp)) { Text("Hello") }
B. Card(padding = 16.dp) { Text("Hello") }
C. Card { modifier = Modifier.padding(16.dp); Text("Hello") }
D. Card(Modifier.padding(16)) { Text("Hello") }
Solution
Step 1: Check correct use of Modifier and padding
Modifier.padding(16.dp) is the correct way to add padding to a composable in Jetpack Compose.
Step 2: Validate syntax correctness
Card(modifier = Modifier.padding(16.dp)) { Text("Hello") } correctly passes modifier = Modifier.padding(16.dp) to Card. Other options misuse parameters or omit .dp unit.
Final Answer:
Card(modifier = Modifier.padding(16.dp)) { Text("Hello") } -> Option A
Quick Check:
Modifier.padding(16.dp) syntax = D [OK]
Hint: Use modifier = Modifier.padding(16.dp) inside Card [OK]
Common Mistakes:
Using padding parameter directly on Card
Forgetting .dp unit for padding
Assigning modifier inside Card content block
3. What will be the visual result of this code snippet?
Modifier.padding requires a Dp value like 12.dp, not just 12.
Step 2: Validate other parts
Text inside Card is valid, Modifier.padding works with Card, and background color is optional.
Final Answer:
Missing .dp unit for padding value -> Option B
Quick Check:
Modifier.padding needs .dp unit = B [OK]
Hint: Always add .dp after numeric padding values [OK]
Common Mistakes:
Omitting .dp unit causes compile error
Thinking Card needs background color always
Believing Text can't be child of Card
5. You want to create a Card that has rounded corners, a shadow, and changes background color when clicked. Which approach correctly combines these features?
hard
A. Use Card with elevation for corners, shape for shadow, and no clickable modifier
B. Use Card with padding for shadow, shape for color, and clickable for corners
C. Use Card with background color parameter only and add shadow manually
D. Use Card with shape = RoundedCornerShape(12.dp), elevation = 8.dp, and clickable modifier with background color change inside
Step 2: Combine clickable with background color change
Clickable modifier can be used with state to change background color on click inside Card content.
Step 3: Evaluate options
Use Card with shape = RoundedCornerShape(12.dp), elevation = 8.dp, and clickable modifier with background color change inside correctly uses shape, elevation, and clickable with background color change. Others misuse parameters or miss features.
Final Answer:
Use Card with shape = RoundedCornerShape(12.dp), elevation = 8.dp, and clickable modifier with background color change inside -> Option D
Quick Check:
Shape + elevation + clickable for color change = A [OK]
Hint: Combine shape, elevation, and clickable modifier for interactive Card [OK]
Common Mistakes:
Confusing elevation with padding for shadow
Misusing shape for background color
Forgetting clickable modifier for interaction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.