Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.
You won't be editing any XML layouts or using the Layout Editor. Instead, you will call composable functions to define what elements you want, and the Compose compiler will do the rest.
Use
Column
to place items vertically on the screen.
use
Row
to place items horizontally on the screen. Both Column
and Row
support
configuring the alignment of the elements they contain.
In many cases, you can just use Compose's standard layout elements.
Here is the sample code ✌:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComoseTutorialsTheme {
// A surface container using the 'background' color from the theme
ActorCard(Actor("John Wick", "Actor", 49))
}
}
}
}
data class Actor(val name:String, val profession: String, val age: Int)
@Composable
fun ActorCard(actor: Actor) {
Card(modifier = Modifier.fillMaxWidth().padding(all = 4.dp)) {
Row(modifier = Modifier.padding(all = 8.dp)) {
Image(
painter = painterResource(R.drawable.profile),
contentDescription = null,
modifier = Modifier
.size(40.dp)
.clip(CircleShape)
.border(2.dp, MaterialTheme.colorScheme.primary, CircleShape),
alignment = Alignment.BottomCenter
)
Spacer(modifier = Modifier.width(4.dp))
Column {
Row {
Text(
text = actor.name,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.secondary
)
Text(
text = ", "+actor.age.toString(),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.secondary
)
}
Spacer(modifier = Modifier.height(4.dp))
Text(text = actor.profession)
}
}
}
}class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
JetpackComoseTutorialsTheme {
// A surface container using the 'background' color from the theme
ActorCard(Actor("John Wick", "Actor", 49))
}
}
}
}
data class Actor(val name:String, val profession: String, val age: Int)
@Composable
fun ActorCard(actor: Actor) {
Card(modifier = Modifier.fillMaxWidth().padding(all = 4.dp)) {
Row(modifier = Modifier.padding(all = 8.dp)) {
Image(
painter = painterResource(R.drawable.profile),
contentDescription = null,
modifier = Modifier
.size(40.dp)
.clip(CircleShape)
.border(2.dp, MaterialTheme.colorScheme.primary, CircleShape),
alignment = Alignment.BottomCenter
)
Spacer(modifier = Modifier.width(4.dp))
Column {
Row {
Text(
text = actor.name,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.secondary
)
Text(
text = ", "+actor.age.toString(),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.secondary
)
}
Spacer(modifier = Modifier.height(4.dp))
Text(text = actor.profession)
}
}
}
}
Sample output :
Happy Coding ✌.