Power of Jetpack Compose in Android Development

sruthi sankar
3 min readJun 17, 2024

--

Jetpack Compose is a modern toolkit for building native Android apps using Kotlin. It’s fully declarative, which means you describe what the UI should look like, and Compose handles the rendering and updates. It simplifies UI development with:

  • Declarative UI:
    You define the UI in terms of its state, and Compose manages the updates.
  • Kotlin-Based:
    Leveraging Kotlin’s features, Compose allows for concise, readable, and expressive code.

2. Understanding and Creating Composables in Jetpack Compose

Composables are the building blocks of Jetpack Compose. They are Kotlin functions annotated with `@Composable`, defining a piece of the UI.

Creating a Composable:

1. Define a Function: Annotate it with `@Composable`.
2. Add UI Elements: Use built-in composables like `Text()`, `Button()`, or custom ones.
3. Parameters for Customization: Allow customization through parameters.
4. Compose Together: Use custom composables within others for reusable, modular design.
Example:

@Composable
fun CustomText(text: String) {
Text(text = text, style = TextStyle(fontSize = 20.sp, color = Color.Red))
}
@Composable
fun MyApp() {
Column {
CustomText(text = "Hello, World!")
CustomText(text = "Welcome to Jetpack Compose!")
}
}

3. Jetpack Compose: Columns and Rows

Columns:- arrange items vertically.
Example:

@Composable
fun MyColumn() {
Column {
Text("First item")
Text("Second item")
Text("Third item")
}
}

Rows:- arrange items horizontally.
Example:

@Composable
fun MyRow() {
Row {
Text("First item")
Text("Second item")
Text("Third item")
}
}

4. Understanding Text Composables in Jetpack Compose

TextFields:- allow user input.
Example:

@Composable
fun MyTextField() {
TextField(
value = "",
onValueChange = {},
label = { Text("Enter your name") }
)
}

5. The Preview Composable

Preview:- lets you see what your composable looks like without running the app.
Example:

@Preview
@Composable
fun PreviewMyButton() {
MyButton()
}

6. The Button Composable

Button:- is a clickable element.
Example:

@Composable
fun MyButton() {
Button(onClick = {
println("Button was clicked!")
}) {
Text("Click me")
}
}

7. Understanding Context

Context:- connects app parts to the system, used for accessing resources, starting activities, and showing toast messages.
Example:

@Composable
fun ShowToastButton() {
val context = LocalContext.current
Button(onClick = {
Toast.makeText(context, "Button clicked!", Toast.LENGTH_SHORT).show()
}) {
Text("Click me")
}
}

8. Understanding the Toast Class

Toast:- shows a brief message on the screen.
Example:

val toast = Toast.makeText(context, "Button clicked!", Toast.LENGTH_SHORT)
toast.show()

9. The Box Composable

Box:- allows stacking elements on top of each other.
Example:

@Composable
fun IconButton() {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.size(100.dp)
) {
Icon(imageVector = Icons.Default.Star, contentDescription = null)
Text("Star")
}
}

10. The Icon Composable

Icon:- adds small pictures or symbols.
Example:

@Composable
fun StarIcon() {
Icon(
imageVector = Icons.Default.Star,
contentDescription = "Star Icon"
)
}

11. Creating a Dropdown Menu

Dropdown Menu:- shows more options when clicked.
Example:

@Composable
fun DropdownMenuExample() {
var expanded by remember { mutableStateOf(false) }
Box {
IconButton(onClick = { expanded = !expanded }) {
Icon(imageVector = Icons.Default.ArrowDropDown, contentDescription = null)
}
DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
DropdownMenuItem(onClick = { /* Do something */ }) {
Text("Option 1")
}
DropdownMenuItem(onClick = { /* Do something */ }) {
Text("Option 2")
}
}
}
}

12. Understanding Parent Containers

Parent Containers:- hold and organize other UI components.
- Column:- Arranges items vertically.
- Row:- Arranges items horizontally.
- Box:- Layers items on top of each other.
Example using Column:-

@Composable
fun SimpleScreen() {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
Image(painter = painterResource(id = R.drawable.ic_launcher_foreground), contentDescription = null)
Button(onClick = { /* Do something */ }) {
Text("Click me")
}
}
}

13. Space vs. Padding

- Space: The empty area between different elements.
- Padding: Space inside an element’s borders, around its content.

Padding Modifier Example:-

Text("Hello", modifier = Modifier.padding(16.dp))

Spacer Elements:-

Spacer:- creates empty space between elements.
Example:

Column {
Text("Item 1")
Spacer(modifier = Modifier.height(20.dp))
Text("Item 2")
}

--

--