Android Navigation - How to navigate between activities in Android Studio by using Kotlin.

Last updated Dec 16, 2021

In this android example, we will see how to navigate one screen to another screen with Android Intents in Android Studio by Kotlin code.

Implementation:

Create a new Project in android studio.

Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

 

Go to activity_first.xml file and add the following code

<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#E8EFA3"
    tools:context=".FirstActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to Second Screen"></Button>

</LinearLayout>

 

 Go to FirstActivity.kt file and add the following code

val btn1 = findViewById(R.id.btn1)

 

        btn1.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }

 

Create a new empty activity 

app > java > package name > right-click > New > Activity /Empty Activity > enter name(SecondActivity.kt) > Finish.

 

Add following code in the activity_second.xml file

   <LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#A8B80A"
    tools:context=".AndroidNavigation.SecondActivity">

    <Button
        android:id="@+id/btn2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to First Screen"></Button>

</LinearLayout>

 

Open SecondActivity.kt file and add the following code

val btn2 = findViewById(R.id.btn2)

 

        btn2.setOnClickListener{
            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }

 

By using Intent we wil navigate the activity from first to second screen.
Run the app in your emulator or real device, you will see the following output.

 

OUTPUT:

Android Navigation Example


 

Fig. First Screen UI
 

Android Navigation Example


Fig. Second Screen UI

 

Complete Source code of Android Navigation Example:

activity_first.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#E8EFA3"
    tools:context=".FirstActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to Second Screen"></Button>

</LinearLayout>

 

FirstActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class FirstActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        val btn1 = findViewById(R.id.btn1)

 

        btn1.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

 

activity_second.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#A8B80A"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/btn2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to First Screen"></Button>

</LinearLayout>

 

SecondActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        val btn2 = findViewById(R.id.btn2)

 

        btn2.setOnClickListener{
            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }
    }
}


 

Conclusion: We have covered how to navigate between activities using intent in Android with kotlin language.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#E8EFA3"
    tools:context=".FirstActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to Second Screen"></Button>

</LinearLayout>

 

FirstActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class FirstActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        val btn1 = findViewById(R.id.btn1)

 

        btn1.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

 

activity_second.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="#A8B80A"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/btn2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Go to First Screen"></Button>

</LinearLayout>

 

SecondActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        val btn2 = findViewById(R.id.btn2)

 

        btn2.setOnClickListener{
            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }
    }
}


 

Conclusion: We have covered how to navigate between activities using intent in Android with kotlin language.

-->
Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2189 Views