Step-by-Step Guide: Building a Simple Android App with Kotlin

๐Ÿ›  Step 1: Set Up Your Android Project

  1. Open Android Studio (latest stable version recommended).

  2. Click on File โ†’ New โ†’ New Project.

  3. Choose Empty Activity.

  4. Name your project (e.g., HelloWorld).

  5. Select Kotlin as the language.

  6. Finish and wait for Gradle to set up your project.


๐Ÿ“„ Step 2: Design the UI

Open the layout file:

app/src/main/res/layout/activity_main.xml

Replace its content with:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_test_button"
        android:text="@string/test_message"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

๐Ÿ“ Step 3: Add a String Resource

Open:

app/src/main/res/values/strings.xml

Add:

<string name="test_message">Click Me!</string>

๐Ÿงฉ Step 4: Add Logic with Kotlin

Open:

app/src/main/java/com/helloworld/MainActivity.kt

Replace with:

package com.helloworld

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    val mTestButton by lazy { findViewById<Button>(R.id.btn_test_button) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        mTestButton.setOnClickListener {
            Toast.makeText(this, "Hello world", Toast.LENGTH_SHORT).show()
        }
    }
}

โš™๏ธ Step 5: Check AndroidManifest.xml

Open:

app/src/main/AndroidManifest.xml

Make sure it includes your launcher activity:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloWorld"
        tools:targetApi="31">
        <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>

โ–ถ๏ธ Step 6: Build & Run

  • Connect an Android device or use an emulator.

  • Click Run (green โ–ถ button) in Android Studio.

  • Tap the button and see the toast: Hello world.


๐Ÿ“ What is RelativeLayout and why use it?

RelativeLayout is a view group that lets you place child views relative to each other or to the parent.

Common attributes:

  • android:layout_centerInParent="true": centers the view in parent.

  • android:layout_below="@id/otherView": places below another view.

  • android:layout_alignParentTop="true": aligns to parent top.

It helps simplify layouts instead of nesting multiple LinearLayouts.


๐ŸŽ‰ Congratulations!

You've built your first Kotlin Android app!
Feel free to explore further, and follow for more tutorials!


0
Subscribe to my newsletter

Read articles from Valliappan Periannan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Valliappan Periannan
Valliappan Periannan

Started my career as a mobile app intern in 2017 at a dynamic startup. Specialized in Android Kotlin app development, later expanded skills to Swift and React Native. ๐Ÿ“ฑ๐Ÿ’ป Passionate about creating seamless user experiences and staying at the forefront of mobile technology trends. I thrive on challenges and love turning innovative ideas into reality through code. Experienced in collaborating with cross-functional teams and translating complex requirements into elegant solutions. Excited about the potential of technology to make a positive impact on people's lives. Always up for a coding challenge or exploring the next big thing in tech. When I'm not coding, you can find me immersed in a good book, capturing moments through my lens, or conquering new trails. Let's embark on this coding journey together! โœจ๐Ÿš€