This application is a demonstration of how to navigate from one page to another. In this application, we have four pages and each page will have buttons to navigate from one page to another. The video of building this application is given below
Step 1: Start an android project.
Step 2: Open the strings.xml file and write the following code.
<resources>
<string name="app_name">Page Navigation demo</string>
<string name="txt_First_Page">Welcome to First Page</string>
<string name="txt_Second_Page">Welcome to Second Page</string>
<string name="txt_Third_Page">Welcome to Third Page</string>
<string name="txt_Fourth_Page">Welcome to Fourth Page</string>
<string name="btn_First_Page">Home Page</string>
<string name="btn_Last_Page">Last Page</string>
<string name="btn_Next_Page">Next Page</string>
<string name="btn_Previous_Page">Previous Page</string>
</resources>
Step 3: Apart from main_activity.xml, add three more pages namely Second_Page, Third_Page, Fourth_Page. Design all the four pages as shown in the figure.
Page 1: 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">
<TextView
android:layout_width="325dp"
android:layout_height="40dp"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="100dp"
android:text="@string/txt_First_Page"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_p1_next"
android:layout_width="149dp"
android:layout_height="100dp"
android:layout_marginBottom="161dp"
android:layout_marginEnd="37dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="37dp"
android:layout_marginStart="43dp"
android:background="#FFFFFF"
android:text="@string/btn_Next_Page"
android:textSize="24sp"
app:backgroundTint="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_p1_last"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn_p1_last"
android:layout_width="162dp"
android:layout_height="100dp"
android:layout_marginBottom="161dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:text="@string/btn_Last_Page"
android:textSize="24sp"
app:backgroundTint="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Open MainActivity.java file and write the following code.
package com.example.part_b1_page_navigation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_nxt=(Button)findViewById(R.id.btn_p1_next);
Button btn_last=(Button)findViewById(R.id.btn_p1_last);
btn_nxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_nxt=new Intent(MainActivity.this, Second_Page.class);
startActivity(in_nxt);
}
});
btn_last.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_last=new Intent(MainActivity.this,Fourth_Page.class);
startActivity(in_last);
}
});
}
}
Step 5: Open Second_Page.java file and write the following code.
package com.example.part_b1_page_navigation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Second_Page extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second__page);
Button btn_previous=(Button)findViewById(R.id.btn_p2_previous);
Button btn_next=(Button)findViewById(R.id.btn_p2_next);
Button btn_last=(Button)findViewById(R.id.btn_p2_last);
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_previous=new Intent(Second_Page.this,MainActivity.class);
startActivity(in_previous);
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_next=new Intent(Second_Page.this,Third_Page.class);
startActivity(in_next);
}
});
btn_last.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_last=new Intent(Second_Page.this,Fourth_Page.class);
startActivity(in_last);
}
});
}
}
Step 6: Open Third_Page.java file and write the following code.
package com.example.part_b1_page_navigation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Third_Page extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third__page);
Button btn_home=(Button)findViewById(R.id.btn_p3_home);
Button btn_previous=(Button)findViewById(R.id.btn_p3_previous);
Button btn_next=(Button)findViewById(R.id.btn_p3_next);
btn_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_home=new Intent(Third_Page.this,MainActivity.class);
startActivity(in_home);
}
});
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_previous=new Intent(Third_Page.this,Second_Page.class);
startActivity(in_previous);
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_next=new Intent(Third_Page.this,Fourth_Page.class);
startActivity(in_next);
}
});
}
}
Step 7: Open Fourth_Page.java file and write the following code.
package com.example.part_b1_page_navigation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Fourth_Page extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth__page);
Button btn_previous=(Button)findViewById(R.id.btn_p4_previous);
Button btn_home=(Button)findViewById(R.id.btn_p4_home);
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_previous=new Intent(Fourth_Page.this,Third_Page.class);
startActivity(in_previous);
}
});
btn_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in_home=new Intent(Fourth_Page.this,MainActivity.class);
startActivity(in_home);
}
});
}
}
Step 8: Run the project.