How to navigate from one activity to another
activity in android. ? We will learn this using Java and Kotlin in this
article.
Create a Project
Lets start by creating a new project choose your
language as whether java or kotlin. we will discuss both here.
Create Activities
As the MainActivity will be create by default, we
need to create one more activity called “secondActivity.xml”.We are creating
only a button named “Next”, on the button click we want to configure the
navigation to the second activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_activity"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:layout_below="@id/editTExt"/>
</RelativeLayout>
second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="35sp"/>
</RelativeLayout>
Create Class files
This time we need to create one more class files
for “second_activity.xml” called “SecondActivity.java” or “SecondActivity.kt”
based on the programming language you have chosen on previous steps. Coding is
doing only on the button click in the MainActivity class file only. Here we are
explaining for both kotlin and java. let’s start from kotlin.
MainActivity.kt – find below code if you are using Kotlin as the language
package com.zayantech.demoappkotlin
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<EditText>(R.id.editTExt)
val btnNext = findViewById<Button>(R.id.btnNext)
btnNext.setOnClickListener {
val intent = Intent(this,SecondActivity::class.java)
startActivity(intent)
}
}
}
MainActivity.java - if you using java as your programming language.
package com.zayantech.demoappjava;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btnNext;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = findViewById(R.id.btnNext);
editText = findViewById(R.id.editTExt);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
}
Add Acitivity in AndroidManifiest
As we added one
activity (second activity), it is mandatory to add the activity in
AndroidManifest file.
change your
manifest files by adding the below piece of code just before </application>
table closing.
<activity android:name=".SecondActivity"/>
Run the Project
As we have done
all the steps which required for the navigation between activities, this is the
time to test the project and enjoy. Hope you like it.