Open New Activity on Button Click Using Kotlin

start a new activity on button click
Start Activity

Read later
Save important notes and articles from internet using arusaquotes app.

Create Project

Open Android Studio and create a new project. After gradle build successful, go to File > New > Activity > Empty Activity. Give the activity name NewActivity and Finish to create one more activity.

XML Code

Go to app > resource > layout > activity_main.xml. Add a button and a supportive textview. Here is the final xml code of activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/supportText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:gravity="center"
        android:text="Click on button to open new activity."
        android:textColor="@color/black"
        android:textSize="28sp" />

    <Button
        android:id="@+id/openButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open"
        android:textSize="24sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

Go to app > resource > layout > activity_new.xml. Add a textview. Here is the activity_new.xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".NewActivity">

    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:gravity="center"
        android:text="Welcome to NewActivity"
        android:textColor="@color/black"
        android:textSize="28sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

Kotlin Code

Go to app > java > com.example.startactivity > MainActivity.kt and put the following code after initializing button.

//Open new activity on button click
        openButton.setOnClickListener {
            val intent = Intent(this, NewActivity::class.java)
            startActivity(intent)
        }

Here is the final Kotlin code MainActivity.kt

package com.example.startactivity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    private lateinit var openButton: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        openButton = findViewById(R.id.openButton)

        //Open new activity on button click
        openButton.setOnClickListener {
            val intent = Intent(this, NewActivity::class.java)
            startActivity(intent)
        }
    }
}

Here is the NewActivity.kt

package com.example.startactivity

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

class NewActivity : AppCompatActivity() {
    private lateinit var welcomeText: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)
        welcomeText = findViewById(R.id.welcomeText)
    }
}

Manifest Code

Check if your AndroidManifest.xml looks like the following.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.startactivity">

    <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.StartActivity"
        tools:targetApi="31">
        <activity
            android:name=".NewActivity"
            android:exported="false"
            android:label="NewActivity"
            android:parentActivityName=".MainActivity" />
        <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>

Output

Here is the final output.

open new activity on button click
Start new activity on button click

Learn Android App Development using Kotlin.

Start Learning