Navigate Between Activities in Android Studio

Nidhi Vanjare
The Startup
Published in
2 min readFeb 22, 2021

Switching between pages in an application is one of the basic features of an app. We can do that by adding few lines of code.

For that open android studio and create a new project.

create new project > Empty Activity >Next > Enter name of the project > Finish

Create two activies that we can navigate in using a button.

(We can navigate between pages using pretty much every element, not necessarily button).

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<Button
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is the First Page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity2">


<Button
android:id="@+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is the Second Page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Don’t forget to give id’s to the buttons which we will need in MainActivity.java file.

To navigate from activity_main.xml to activity_main2.xml we have to write the code in MainActivity.java file.

We have to set an onClickListner to the element which we are going to use to navigate between pages (in this case button).

package com.example.navigation;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

Button b1 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = findViewById(R.id.page1);
b1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
}
);

}
}

Now after clicking the button in the first page the second page is opened.

Thank You , Happy Coding!

--

--