Android Navigation - How to navigate between activities in Android Studio by using Kotlin.
Last updated Dec 16, 2021In 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" <Button </LinearLayout> |
Go to FirstActivity.kt file and add the following code
val btn1 = findViewById(R.id.btn1)
btn1.setOnClickListener{ |
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" <Button </LinearLayout> |
Open SecondActivity.kt file and add the following code
val btn2 = findViewById(R.id.btn2)
btn2.setOnClickListener{ |
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:
Fig. First Screen UI
Fig. Second Screen UI
Complete Source code of Android Navigation Example:
activity_first.xml file
<?xml version="1.0" encoding="utf-8"?> <Button </LinearLayout> |
FirstActivity.kt file
import android.content.Intent class FirstActivity : AppCompatActivity() {
btn1.setOnClickListener{ |
activity_second.xml file
<?xml version="1.0" encoding="utf-8"?> <Button </LinearLayout> |
SecondActivity.kt file
import android.content.Intent class SecondActivity : AppCompatActivity() {
btn2.setOnClickListener{ |
Conclusion: We have covered how to navigate between activities using intent in Android with kotlin language.
Article Contributed By :
|
|
|
|
3002 Views |