Making your first Android app helps you learn how to create simple screens and show messages. It is the first step to building useful mobile apps.
First Android app in Android Kotlin
Introduction
Syntax
Android Kotlin
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
This code defines the main screen of your app.
setContentView tells Android which layout file to show.
Examples
activity_main.Android Kotlin
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
Android Kotlin
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Android!" android:textSize="24sp" android:layout_gravity="center" />
Sample App
This app shows a white screen with the text "Hello, Android!" centered in the middle.
Android Kotlin
package com.example.firstapp import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } /* activity_main.xml layout file content: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Android!" android:textSize="24sp" android:layout_gravity="center" /> </LinearLayout> */
Important Notes
Always call super.onCreate(savedInstanceState) first in onCreate.
Layouts are defined in XML files inside the res/layout folder.
Use TextView to show text on the screen.
Summary
Your first Android app has a main activity that loads a layout.
The layout file defines what the user sees on the screen.
This simple app shows a greeting message centered on a white background.
Practice
1. What is the role of the
MainActivity class in a simple Android app?easy
Solution
Step 1: Understand MainActivity's purpose
MainActivity is the entry point of the app that sets the screen content.Step 2: Identify its function in UI
It loads the layout file which defines what the user sees and handles user actions.Final Answer:
It controls what the user sees by loading the layout and handling interactions. -> Option AQuick Check:
MainActivity controls UI = A [OK]
Hint: MainActivity sets the screen content and user interaction [OK]
Common Mistakes:
- Confusing MainActivity with data storage classes
- Thinking MainActivity handles network tasks
- Believing MainActivity compiles code
2. Which of the following is the correct way to set the content view in
MainActivity using Kotlin?easy
Solution
Step 1: Recall setContentView syntax
In Kotlin, setContentView expects a layout resource ID, accessed as R.layout.filename.Step 2: Match correct resource reference
R.layout.activity_main correctly points to the layout XML file named activity_main.xml.Final Answer:
setContentView(R.layout.activity_main) -> Option BQuick Check:
Layout resource ID = R.layout.filename [OK]
Hint: Use R.layout.filename to reference layout files [OK]
Common Mistakes:
- Using string names instead of resource IDs
- Confusing layout and id resources
- Including file extensions in code
3. Given this Kotlin code in
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
What will the user see when the app runs?medium
Solution
Step 1: Understand onCreate behavior
onCreate is called when the app starts and sets the UI with setContentView.Step 2: Effect of setContentView
It loads the layout file activity_main.xml, so the user sees that layout on screen.Final Answer:
The layout defined in activity_main.xml displayed on screen -> Option DQuick Check:
setContentView loads layout = C [OK]
Hint: onCreate sets UI with setContentView(layout) [OK]
Common Mistakes:
- Thinking onCreate needs a return type
- Assuming no UI appears without extra code
- Confusing layout with id resources
4. What is wrong with this
MainActivity code snippet?
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
super.onCreate(savedInstanceState)
}medium
Solution
Step 1: Check onCreate call order
super.onCreate must be called first to properly initialize the activity.Step 2: Identify correct order
Calling setContentView before super.onCreate causes errors or unexpected behavior.Final Answer:
super.onCreate(savedInstanceState) must be called before setContentView -> Option CQuick Check:
super.onCreate first = B [OK]
Hint: Always call super.onCreate first inside onCreate [OK]
Common Mistakes:
- Calling setContentView before super.onCreate
- Using non-nullable Bundle parameter
- Expecting onCreate to return a value
5. You want your first Android app to show a greeting message centered on a white background. Which layout XML snippet achieves this?
hard
Solution
Step 1: Check layout size and background
uses match_parent for width and height and sets background to white (#FFFFFF).Step 2: Verify text centering
android:gravity=\"center\" centers the TextView inside the LinearLayout.Final Answer:
-> Option AQuick Check:
Center gravity + white background = A [OK]
Hint: Use LinearLayout with gravity=center and background=#FFFFFF [OK]
Common Mistakes:
- Using wrap_content for parent layout size
- Missing gravity=center for centering
- Not setting background color
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
