Skip to main content

Command Palette

Search for a command to run...

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

Step-by-Step Instructions for Building a Basic Android App Using Kotlin

Updated
β€’2 min read
Step-by-Step Guide: Building a Simple Android App with Kotlin
V

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! βœ¨πŸš€

πŸ›  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!