Skip to main content
Kotlin Basics
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 Activity is.
  • Bind an XML UI button to Kotlin logic.
  • Use an Intent to 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?

An Activity represents a single screen with a user interface. If your app has a Login Screen and a Home Screen, you have two Activities.
kotlin
12345678910111213
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

// MainActivity inherits from AppCompatActivity
class MainActivity : AppCompatActivity() {

    // onCreate is the entry point for this screen (like fun main)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Connects this Kotlin code to the XML visual layout!
        setContentView(R.layout.activity_main) 
    }
}

6. Binding UI to Kotlin

Suppose your XML has a button with the ID btnSubmit. How do you make it do something when clicked?
kotlin
123456789101112131415161718
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // 1. Find the button from the XML
        val submitButton = findViewById<Button>(R.id.btnSubmit)
        
        // 2. Attach a Click Listener using a trailing lambda!
        submitButton.setOnClickListener {
            // 3. Show a pop-up message (Toast)
            Toast.makeText(this, "Button Clicked!", Toast.LENGTH_SHORT).show()
        }
    }
}

7. Navigating Between Screens (Intents)

To move from MainActivity to DetailsActivity, Android uses an object called an Intent. It represents your "intention" to do something.
kotlin
12345678910111213141516171819
import android.content.Intent

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        val nextButton = findViewById<Button>(R.id.btnNext)
        
        nextButton.setOnClickListener {
            // Create an Intent stating we want to go to DetailsActivity
            // Note the use of ::class.java to interoperate with Android's Java core
            val intent = Intent(this, DetailsActivity::class.java)
            
            // Execute the navigation
            startActivity(intent)
        }
    }
}

8. Common Mistakes

  • Forgetting to declare Activities in the Manifest: Every single Activity must be registered in the AndroidManifest.xml file. 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* calling setContentView(). The app will crash because the XML hasn't been loaded into memory yet!

9. Best Practices

  • Use ViewBinding: findViewById is considered legacy and is prone to null crashes. Learn and enable "ViewBinding" in your build.gradle file, which automatically generates safe Kotlin properties for every ID in your XML.

10. Exercises

*(Theoretical)*
  1. 1. Describe the relationship between activity_main.xml and MainActivity.kt.
  1. 2. Write the syntax to create an Intent navigating from Login to Home.

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 Intent in Android.
  • Q: What is the difference between an Activity and an XML Layout?

13. Summary

Android development relies heavily on Kotlin's concise syntax. By understanding how Activities govern logic, how XML handles UI, and how to bind them together using click listeners and Intents, you possess the foundational knowledge required to build and navigate through a multi-screen mobile application.

14. Next Chapter Recommendation

Apps need data. In Chapter 25: Kotlin Database Programming, we will explore how to store data permanently on a device using SQLite and Google's recommended database library: Room.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·