Android  

Jetpack Compose and Its Basic Building Blocks

Introduction

Jetpack Compose is a UI toolkit introduced by Google that allows developers to build Android UIs using Kotlin code. It's designed to be,

  • Declarative: You describe what the UI should look like based on the current state.
  • Reactive: The UI automatically updates when the state changes.
  • Flexible: You can create custom UI components easily.
  • Modern: Built entirely in Kotlin and integrates smoothly with Android Jetpack libraries.

Basic Building Blocks of Jetpack Compose

1. Composable Functions (@Composable)

At the heart of Jetpack Compose are composable functions, marked with the @Composable annotation. These functions are responsible for building your UI components.

@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!")
}

2. Layouts

Layouts are used to arrange UI elements on the screen. Common layout composables include.

  • Column: Arranges children vertically.
  • Row: Arranges children horizontally.
  • Box: Stacks children on top of each other.
  • LazyColumn / LazyRow: Efficient scrolling lists.
Column {
    Text("Ankit")
    Text("Singh")
}

3. Modifiers

Modifiers allow you to style and configure UI elements. They can control.

  • Size
  • Padding
  • Background
  • Click behavior
  • Alignment, and more
Text(
    text = "Hello Compose",
    modifier = Modifier
        .padding(16.dp)
        .background(Color.Yellow)
)

4. State Management

Compose is state-driven. You don't manually update the UI — instead, the UI reacts automatically when state changes.

To store and observe state, you use remember and mutableStateOf.

@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }

    Button(onClick = { count++ }) {
        Text("Count: $count")
    }
}

5. Text and Image

  • Text is used to show strings on screen.
  • Image displays images from resources or URLs.
Text("Welcome to Compose!")

Image(
    painter = painterResource(R.drawable.ic_launcher_foreground),
    contentDescription = "App Icon"
)

6. Buttons and Inputs

Compose includes many built-in interactive components, like,

  • Button
  • IconButton
  • TextField
  • Checkbox
  • Switch
Button(onClick = { /* handle click */ }) {
    Text("Click Me")
}

7. Theming with Material Design

Jetpack Compose includes Material Design out of the box. You can customize.

  • Colors
  • Typography
  • Shapes
  • Elevation
MaterialTheme {
    Text("Styled Text", style = MaterialTheme.typography.titleLarge)
}

Conclusion

Jetpack Compose is a game-changer for Android development. By learning its basic building blocks, you’ll be able to build powerful UIs faster, with less code and more flexibility. Whether you're building a to-do list app or a full-featured e-commerce platform, Compose gives you the tools to create modern, maintainable, and beautiful user interfaces — all in Kotlin.