KtDevLog
  • Home
  • Jetpack Compose
  • Kotlin Fundamentals
  • Android Studio
No Result
View All Result
KtDevLog
  • Home
  • Jetpack Compose
  • Kotlin Fundamentals
  • Android Studio
No Result
View All Result
KtDevLog
No Result
View All Result
Your First Jetpack Compose Function Explained

Your First Jetpack Compose Function Explained

Md Sharif Mia by Md Sharif Mia
April 21, 2026
in Jetpack Compose
0
2
Share on FacebookShare on PinterestShare on X

If you’ve just created a new Android project in Android Studio and stared at the auto-generated code wondering “what on earth is going on here?” — you’re not alone. That was me a couple of years ago.

Jetpack Compose is Google’s modern way of building Android UIs. Instead of dragging widgets into XML layouts, you write UI in pure Kotlin using special functions. It sounds intimidating at first, but once it clicks, you’ll wonder how you ever tolerated XML.

Let’s walk through exactly what the starter code in your MainActivity.kt is doing — line by line, no assumptions, no skipped steps.

Related Posts

Remember vs rememberSaveable in Jetpack Compose Explained

Remember vs rememberSaveable in Jetpack Compose Explained

May 2, 2026
Jetpack Compose LazyColumn Example: Build Fast Scrolling Lists

Jetpack Compose LazyColumn Example: Build Fast Scrolling Lists

May 1, 2026
Jetpack Compose Navigation Tutorial: Pass Arguments Easily

Jetpack Compose Navigation Tutorial: Pass Arguments Easily

April 29, 2026
Row in Jetpack Compose: 5 Essential Layout Tips

Row in Jetpack Compose: 5 Essential Layout Tips

April 23, 2026
Your First Jetpack Compose Function Explained

Table of Contents

  • Breaking Down the Greeting Function
    • The @Composable Annotation
    • The Text Composable
  • Understanding MainActivity and setContent
    • ComponentActivity and onCreate
    • enableEdgeToEdge()
    • setContent { }
    • FirstJetpackComposeTheme { }
    • Scaffold
    • Calling Greeting
  • The @Preview Function — Your Best Friend in Compose
  • FAQ
    • What does @Composable actually do under the hood?
    • Why does Modifier have a capital M?
    • Can I have multiple @Preview functions in one file?
    • What’s the difference between ComponentActivity and AppCompatActivity?
    • Do I always need Scaffold?
  • Conclusion

Before anything else, you need to understand one word: Composable.

In Jetpack Compose, a Composable function is a special kind of Kotlin function that describes what your UI should look like. You mark it with the @Composable annotation, and Android knows to treat it as a UI building block — not just regular logic.

Think of it like a LEGO brick. One Composable can contain other Composables, and together they build your entire screen. According to the official Android documentation, Compose works by calling these functions repeatedly to “recompose” (redraw) the UI whenever data changes.

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

This is your very first Composable function. Let’s unpack every part of it.

Breaking Down the Greeting Function

The @Composable Annotation

Kotlin
@Composable
Kotlin


This single annotation is what transforms an ordinary Kotlin function into a UI function. Without it, Android won’t know this function is supposed to render something on screen. It’s a compile-time signal that tells the Compose compiler: “treat this differently.”

You’ll put @Composable above every function that draws UI. If you forget it and try to call a Composable from inside a non-Composable function, Android Studio will immediately give you an error. It enforces good structure from day one.

The Function Signature

Kotlin
fun Greeting(name: String, modifier: Modifier = Modifier) {
Kotlin

This function takes two parameters:

  • name: String — a piece of text you want to display. In the screenshot, "KtDevLog" is passed in, which is why the preview shows “Hello KtDevLog!”
  • modifier: Modifier = Modifier — this one trips up a lot of beginners

The Modifier is one of the most important concepts in Jetpack Compose. It’s a chainable object that controls layout properties — things like size, padding, background color, click behavior, and more. By passing it as a parameter with a default value of Modifier (an empty modifier), you give the caller the flexibility to customize this composable’s appearance from outside.

This is a common Compose pattern you’ll use constantly. Most guides gloss over why modifier has a default value — the reason is that it makes the function reusable. You can call Greeting(name = "Alice") without worrying about modifiers, or Greeting(name = "Alice", modifier = Modifier.padding(16.dp)) when you need layout control.

The Text Composable

Kotlin
Text(
    text = "Hello $name!",
    modifier = modifier
)
Kotlin

Text is a built-in Composable from Jetpack Compose. It renders a string on screen — basically the equivalent of a TextView in the old XML world.

The string "Hello $name!" uses Kotlin string templates. The $name part gets replaced at runtime with whatever string you pass in.

The modifier = modifier line passes the modifier this function received down into the Text composable, so any padding or sizing applied from outside actually takes effect on the text.

Understanding MainActivity and setContent

Now let’s look at the top part of the file — the MainActivity class.

Kotlin
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            FirstJetpackComposeTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "KtDevLog",
                        modifier = Modifier.padding(paddingValues = innerPadding)
                    )
                }
            }
        }
    }
}
Kotlin

ComponentActivity and onCreate

MainActivity extends ComponentActivity — a Jetpack-compatible version of the regular Activity. The onCreate function is the entry point that runs when your app starts.

super.onCreate(savedInstanceState) calls the parent class’s setup logic. Always keep this line — skipping it causes crashes.

enableEdgeToEdge()

This is a newer Android API call (added for Android 15 compatibility) that lets your UI draw behind system bars like the status bar and navigation bar. It makes your app look more modern and immersive. You don’t need to configure anything extra — just calling this method enables it.

setContent { }

This is where Jetpack Compose takes over. Instead of using setContentView(R.layout.activity_main) like in the old XML approach, you call setContent and describe your UI inside the lambda using Composable functions.

Everything inside the curly braces is Compose territory.

FirstJetpackComposeTheme { }

This wraps everything in your app’s Material Design theme. Android Studio auto-generates this theme based on your app name. It handles colors, typography, and shapes consistently across your app. Think of it as the styling layer that every screen in your app shares.

Scaffold

Kotlin
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Kotlin

Scaffold is a layout container that implements the basic Material Design visual structure — it handles things like top bars, bottom bars, and floating action buttons. Even if you’re not using those, Scaffold is still useful because it provides innerPadding.

Modifier.fillMaxSize() makes the Scaffold fill the entire screen.

The innerPadding lambda parameter is automatically calculated by Scaffold to account for system UI insets (like the status bar height). You pass it into your content so nothing gets hidden behind system bars.

Calling Greeting

Kotlin
Greeting(
    name = "KtDevLog",
    modifier = Modifier.padding(paddingValues = innerPadding)
)
Kotlin

Here’s where the Greeting function you wrote actually gets used. The name "KtDevLog" is passed in, and innerPadding is applied as padding so the text doesn’t overlap with the status bar.

The @Preview Function — Your Best Friend in Compose

Kotlin
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    FirstJetpackComposeTheme {
        Greeting(name = "KtDevLog")
    }
}
Kotlin

This is a preview-only Composable. The @Preview annotation tells Android Studio to render it in the design panel — which is exactly the “Hello KtDevLog!” box you see on the right side of the screenshot.

The showBackground = true parameter adds a white background to the preview so it looks closer to how it’ll appear on a real screen.

Here’s something most beginner tutorials miss: @Preview functions should never be called from your real app code. They exist only for the IDE preview tool. If you call them elsewhere, you’re essentially hardcoding dummy data into your production UI.

FAQ

What does @Composable actually do under the hood?

The Compose compiler processes @Composable annotations at build time and adds special logic that lets the function participate in recomposition — Android’s process of automatically redrawing UI when state changes. Without this annotation, the function has no connection to Compose’s rendering system.

Why does Modifier have a capital M?

Modifier with a capital M refers to the type (the class), while Modifier as a default value refers to the companion object that gives you an empty starting modifier. Yes, they share the same name — it’s a Kotlin idiom. You’ll see modifier: Modifier = Modifier frequently, and it always means: “this parameter is of type Modifier, and if you don’t provide one, use an empty Modifier.”

Can I have multiple @Preview functions in one file?

Absolutely. You can have as many @Preview functions as you want, each showing a different state or screen size. This is a powerful way to test your UI without running the app on a device or emulator.

What’s the difference between ComponentActivity and AppCompatActivity?

ComponentActivity is the lightweight Jetpack base class designed specifically for Compose. AppCompatActivity is the older base class for apps that use XML layouts and the AppCompat support library. For new Compose-first projects, stick with ComponentActivity.

Do I always need Scaffold?

No, Scaffold isn’t required. You can put any Composable directly inside setContent. But Scaffold is recommended because it handles padding for system bars correctly — especially important after enabling edge-to-edge.

Conclusion

That’s the full picture of your first Jetpack Compose screen. What looks like a simple “Hello World” is actually teaching you the entire foundation of Compose: Composable functions, Modifiers, themes, Scaffold, and previews.

The honest truth? Once this structure clicks, everything else in Compose follows naturally. Start with @Composable. Add a Modifier. Nest your UI components. Preview as you go.

Your next step: try modifying the Greeting function to show a different message, or add a second Text Composable below the first one. Break it, fix it, and own it — that’s how Compose starts to feel like second nature.

Tags: Jetpack Compose function
SharePinTweet
Md Sharif Mia

Md Sharif Mia

Md Sharif Mia is a Kotlin and Android developer with hands-on experience building real-world Android applications using Kotlin, Jetpack Compose, and Firebase. He created KtDevLog to help aspiring Android developers learn through practical, step-by-step tutorials — from writing their first line of Kotlin to shipping complete apps. Through KtDevLog, Sharif shares what actually works in Android development: clean code patterns, common beginner mistakes to avoid, and project-based lessons that go beyond theory. His writing style is direct and beginner-friendly, making complex Android concepts easy to understand for developers at any stage. When he is not writing tutorials, Sharif is experimenting with new Android features, exploring Kotlin best practices, and building apps that solve everyday problems.

Related Posts

Remember vs rememberSaveable in Jetpack Compose Explained
Jetpack Compose

Remember vs rememberSaveable in Jetpack Compose Explained

May 2, 2026

You've built a form. The user carefully types their email address, their name, their...

Jetpack Compose LazyColumn Example: Build Fast Scrolling Lists
Jetpack Compose

Jetpack Compose LazyColumn Example: Build Fast Scrolling Lists

May 1, 2026

Every Android app you've ever used has a list in it. Contacts, messages, products,...

Jetpack Compose Navigation Tutorial: Pass Arguments Easily
Jetpack Compose

Jetpack Compose Navigation Tutorial: Pass Arguments Easily

April 29, 2026

Think about the last app you used on your phone. You tapped a product...

Row in Jetpack Compose: 5 Essential Layout Tips
Jetpack Compose

Row in Jetpack Compose: 5 Essential Layout Tips

April 23, 2026

Picture a restaurant menu. The dish name is on the left. The price is...

Comments 2

  1. Pingback: Kotlin Data Class: copy(), toString() Explained
  2. Pingback: Row in Jetpack Compose: 5 Essential Layout Tips

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

© Copyright 2026 KtDevLog. All Rights Reserved.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Jetpack Compose
  • Kotlin Fundamentals
  • Android Studio

© Copyright 2026 KtDevLog. All Rights Reserved.