CHAPTER 24
Beginner
Kotlin for Android Basics
Updated: May 18, 2026
5 min read
# CHAPTER 24
Kotlin for Android Basics
1. Chapter Introduction
In 2019, Google announced that Android development is "Kotlin-first." While you can still write Android apps in Java, every new API, library, and tutorial released by Google is written in Kotlin. In this chapter, we will bridge the gap between pure Kotlin console applications and visual Mobile Apps. We will cover the basic architecture of an Android App, including Activities, UI Layouts, and Intents.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the role of Android Studio.
- Explain the MVC architecture in Android (XML + Kotlin).
-
Understand what an
Activityis.
- Bind an XML UI button to Kotlin logic.
-
Use an
Intentto navigate between screens.
3. Android Studio vs IntelliJ IDEA
To build Android apps, you must use Android Studio. It is built on top of IntelliJ IDEA, but includes specific tools for Android:- Android Emulator (Virtual Phone).
- SDK Manager (To download different Android versions).
- Visual Layout Editor (To drag and drop UI elements).
4. The Architecture (XML + Kotlin)
Classic Android development separates the visual design from the business logic.-
XML Files (
res/layout/activity_main.xml): Dictates how the screen looks (Buttons, Text colors, Margins).
-
Kotlin Files (
MainActivity.kt): Dictates how the screen behaves (What happens when a button is clicked).
*(Note: Google is currently transitioning to "Jetpack Compose", which allows you to build UIs purely in Kotlin without XML, but understanding the XML system is still mandatory for maintaining millions of existing apps).*
5. What is an Activity?
AnActivity represents a single screen with a user interface. If your app has a Login Screen and a Home Screen, you have two Activities.
kotlin
6. Binding UI to Kotlin
Suppose your XML has a button with the IDbtnSubmit. How do you make it do something when clicked?
kotlin
7. Navigating Between Screens (Intents)
To move fromMainActivity to DetailsActivity, Android uses an object called an Intent. It represents your "intention" to do something.
kotlin
8. Common Mistakes
-
Forgetting to declare Activities in the Manifest: Every single Activity must be registered in the
AndroidManifest.xmlfile. If you create a new Activity but don't add it to the manifest, the app will instantly crash when you try to navigate to it.
-
Null UI Elements: Calling
findViewById*before* callingsetContentView(). The app will crash because the XML hasn't been loaded into memory yet!
9. Best Practices
-
Use ViewBinding:
findViewByIdis considered legacy and is prone to null crashes. Learn and enable "ViewBinding" in yourbuild.gradlefile, which automatically generates safe Kotlin properties for every ID in your XML.
10. Exercises
*(Theoretical)*-
1.
Describe the relationship between
activity_main.xmlandMainActivity.kt.
-
2.
Write the syntax to create an
Intentnavigating fromLogintoHome.
11. MCQs with Answers
Question 1
What is the official IDE for building Android Applications?
Question 2
What language is currently deemed "preferred" and "first-class" by Google for Android?
Question 3
In classic Android architecture, what file format is used to design the visual layout of a screen?
Question 4
What represents a single screen with a user interface in Android?
Question 5
What is the lifecycle method that acts as the entry point when a screen is created?
Question 6
What method connects the Kotlin Activity to the XML layout file?
Question 7
How do you trigger code when a user taps a Button?
Question 8
What small, floating pop-up message is used for quick notifications in Android?
Question 9
What object is used to request navigation from one Activity to another?
Question 10
Where must every Activity be registered to prevent the app from crashing?
12. Interview Questions
-
Q: Explain the purpose of an
Intentin Android.
- Q: What is the difference between an Activity and an XML Layout?