Kotlin Android Intro

Kotlin is the preferred language for Android app development, officially supported by Google since 2017. An Android app is built from components — Activities, Fragments, layouts, and resources — that work together to create the screens and interactions users see on their phones.

How an Android App Is Structured


MyAndroidApp/
├── app/
│   ├── src/
│   │   └── main/
│   │       ├── java/com/myapp/    ← Kotlin source files
│   │       │   └── MainActivity.kt
│   │       ├── res/               ← Resources
│   │       │   ├── layout/        ← XML screen designs
│   │       │   │   └── activity_main.xml
│   │       │   ├── values/
│   │       │   │   └── strings.xml
│   │       │   └── drawable/      ← images and icons
│   │       └── AndroidManifest.xml ← app configuration
│   └── build.gradle.kts           ← dependencies
└── build.gradle.kts               ← project-level config

Activity: The Basic Screen

An Activity is one screen of your app. Every screen you build is usually one Activity (or a Fragment inside one). The MainActivity is the first screen the user sees when the app opens.

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.widget.Button

class MainActivity : AppCompatActivity() {

    // Called when the Activity is first created
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)   // link to XML layout

        val greeting: TextView = findViewById(R.id.greetingText)
        val button: Button     = findViewById(R.id.myButton)

        button.setOnClickListener {
            greeting.text = "Hello, Android with Kotlin!"
        }
    }
}

Layout XML (activity_main.xml)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/greetingText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press the button"
        android:textSize="18sp" />

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say Hello" />

</LinearLayout>

Activity Lifecycle


App starts:
  onCreate()   ← initialize UI, set up data
      │
  onStart()    ← Activity becomes visible
      │
  onResume()   ← Activity is in foreground and interactive

User presses Home or another app opens:
  onPause()    ← Activity losing focus (save quick data)
      │
  onStop()     ← Activity no longer visible

User returns to the app:
  onRestart() → onStart() → onResume()

Activity is destroyed (back button, memory pressure):
  onDestroy()  ← clean up resources
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        println("onCreate: screen created")
    }

    override fun onResume() {
        super.onResume()
        println("onResume: user is interacting")
    }

    override fun onPause() {
        super.onPause()
        println("onPause: losing focus")
    }

    override fun onDestroy() {
        super.onDestroy()
        println("onDestroy: Activity destroyed")
    }
}

View Binding (Modern Approach)

View Binding replaces findViewById with type-safe auto-generated code:

// In build.gradle.kts:
// android { viewBinding { enable = true } }

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.myButton.setOnClickListener {
            binding.greetingText.text = "Hello from View Binding!"
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="My App"
        android:icon="@mipmap/ic_launcher">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

The LAUNCHER intent-filter marks MainActivity as the entry point — the first screen shown when the user taps the app icon.

Leave a Comment

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