Your First Composable
Create and preview a composable function.
Your First Composable is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Your First Composable
A composable is just a Kotlin function marked with @Composable. Let's write one that greets a user by name.
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}The @Composable Annotation
The @Composable annotation tells the compiler this function can emit UI and can call other composables. Without it, you cannot call Text, Button, or other UI functions.
Naming Convention
Composables that emit UI are named with PascalCase nouns, like Greeting or ProfileCard — similar to a class. This signals "this draws something" rather than "this returns a value".
Parameters Drive the UI
A composable takes parameters just like any function. Those parameters are your inputs. Different arguments produce different UI.
@Composable
fun Greeting(name: String) {
Text(text = "Welcome, $name")
}
// Greeting("Sam") and Greeting("Mia") render different textCalling Composables
Composables can only be called from other composables. You compose a screen by nesting calls.
@Composable
fun WelcomeScreen() {
Greeting(name = "Ada")
Greeting(name = "Grace")
}Hosting with setContent
To actually display composables, an Activity calls setContent { } in onCreate. The lambda is the root of your Compose UI.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
WelcomeScreen()
}
}
}Composables Return Unit
Notice composables do not return a UI object. They have return type Unit and emit UI as a side effect of being called. You build UI by calling, not by returning values.
Default Parameters
Kotlin default arguments work great here. Give a parameter a default so callers can omit it.
@Composable
fun Greeting(name: String = "Guest") {
Text(text = "Hello, $name!")
}
// Greeting() shows "Hello, Guest!"Reusing Composables
Because they are functions, composables are reusable. Define Greeting once and call it anywhere with different names. This is how you avoid duplicating UI code.
Keep Them Small
Good composables are small and focused. One composable should describe one logical chunk of UI. Compose larger screens by combining small composables.
Putting It Together
Here is a complete first composable wired into an activity. Running it on a device shows "Hello, Compose!".
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting(name = "Compose")
}
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}Quick Check
What does the @Composable annotation allow a function to do?
Recap
You wrote your first composable. Remember:
- Mark UI functions with
@Composable. - Use PascalCase names.
- Parameters are the inputs that shape the UI.
setContent { }hosts composables in an activity.
Frequently asked questions
Is the “Your First Composable” lesson free?
Yes — the full text of “Your First Composable” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Your First Composable”?
Create and preview a composable function. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Your First Composable” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is Jetpack Compose?
- Your First Composable
- Text, Image, and Button
- Previews and Hot Reload