Latest News

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"><Buttonandroid: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.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.content.Intentimport android.view.Viewimport android.widget.Buttonclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//initializeval mButton = findViewById<View>(R.id.button) as Button//handle onClickmButton.setOnClickListener {//intent to start NewActivitystartActivity(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"><TextViewandroid:text="New Activity"android:textSize="30sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

NewActivity.kt
package com.blogspot.devofandroid.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleclass 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)

0 Response to "How To Move From One Activity To Another Activity In Android Studio (Kotlin)"