How to move From one Activity to another Activity in Android Studio (Kotlin)

      DESCRIPTION      

In this tutorial we will learn how to open new Activity when a Button is clicked using Android Studio in Kotlin language. When button in an activity(Main Activity) is clicked the new activity(New Activity) will be opened..

      VIDEO      


      SOURCE CODE      

Step 2: Create a new Activity(e.g. Empty Activity)

How to move From one Activity to another Activity in Android Studio (Kotlin)

Step 3: Code

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open New Activity" />

</LinearLayout>

MainActivity.kt
package com.blogspot.devofandroid.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //initialize
        val mButton = findViewById<View>(R.id.button) as Button
        //handle onClick
        mButton.setOnClickListener {
            //intent to start NewActivity
            startActivity(Intent(this@MainActivity, NewActivity::class.java))
        }

    }
}

activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".NewActivity">

    <TextView
        android:text="New Activity"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

NewActivity.kt
package com.blogspot.devofandroid.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class NewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)
    }
}

Step 4: Run Project

Output
How to move From one Activity to another Activity in Android Studio (Kotlin)

Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)