This guide explains how to create an Android app using Kotlin in Android Studio. It includes setting up the project, folder structure, adding a splash screen, UI, dependencies, and running the app.
Why Use Kotlin for Android Development
- Concise and readable code
- Safer with reduced null pointer exceptions
- Interoperable with Java
- Modern features like coroutines for background tasks
- Officially recommended by Google
Step 1: Install and Set Up Android Studio
- Download and install Android Studio from the official website.
- Launch Android Studio and complete the setup.
- Install necessary SDKs when prompted.
Step 2: Create a New Android Project
- Open Android Studio and click “Start a new Android Studio project.”
- Select “Empty Activity” and click “Next.”
- Configure the project:
- App Name: MyFirstApp
- Package Name: com.example.myfirstapp
- Save Location: Choose a folder
- Language: Kotlin
- Minimum SDK: API 23 (Android 6.0)
- Click “Finish.”
Step 3: Create the AndroidManifest.xml File
Located at app/src/main/AndroidManifest.xml. If missing, create it manually
- Update
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="My First App"
android:theme="@style/Theme.MyFirstApp">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
Step 4: Implement a Splash Screen
- Right-click on
java/com/example/myfirstapp/. - Select “New > Kotlin Class/File.”
- Name it
SplashActivity. - Add the following code.
- Update
SplashActivity.kt:
package com.example.myfirstapp
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 2000)
}
}
Step 5: Create the Splash Screen Layout
- Navigate to
res/layout/. - If
activity_splash.xmlis missing, right-click onlayout/→ “New > Layout Resource File.” - Name it
activity_splash.xml. - Add the following XML code.
- Update
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
Step 6: Implement MainActivity.kt
- Right-click on
java/com/example/myfirstapp/. - Select “New > Kotlin Class/File.”
- Name it
MainActivity. - Add the following Kotlin code.
- Create
MainActivity.kt:
package com.example.myfirstapp
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.textView)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
textView.text = "Button Clicked!"
Toast.makeText(this, "Button Clicked!", Toast.LENGTH_SHORT).show()
}
}
}
Step 7: Set the App Icon
- Prepare a 512×512 PNG image.
- In Android Studio, right-click
res/mipmap/→ “New > Image Asset.” - Select the image and set it as the app launcher icon.
- Click “Next > Finish.”
Step 8: Add Dependencies
- Open
build.gradle (Module: app). - Add the following inside
dependencies {}.
- Update
build.gradle (Module: app)dependencies:
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
}
- Click “Sync Now” to apply changes.
Step 9: Run the App
- Open Device Manager.
- Create a new virtual device or connect a physical phone via USB.
- Click “Run” to launch the app.
Step 10: Build and Export the APK
- Click “Build > Build Bundle(s) / APK(s) > Build APK(s).”
- Wait for the build process to complete.
- Click “Locate APK” to find the generated
.apkfile. - Transfer and install the
.apkon your phone.