Beginner

Switch between Activities in Android - Best Step by Step Guide

In this lesson, you add some code to the MainActivity that starts a new activity to display. Also switch between two activities. Here is my video where you can get the full code with my live codes. to watch this video You can understand what exactly you are going to code

2022-04-06
Share this

This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button click_first_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_first_btn = findViewById(R.id.click_first_btn);
        click_first_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent i = new Intent(getApplicationContext(),SecondActivity.class);
                 startActivity(i);
            }
        });
    }
}

Now create another Activity and named that SecondActivity.java. Add this code to that

public class SecondActivity extends AppCompatActivity {

    Button click_second_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        click_second_btn = findViewById(R.id.click_second_btn);
        click_second_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SecondActivity.this,MainActivity.class);
                startActivity(i);
            }
        });

    }
}

This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java

<?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:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Im First Activity" />

    <Button
        android:id="@+id/click_first_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to go Second"/>

</LinearLayout>

This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java

<?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:orientation="vertical"
    android:gravity="center"
    tools:context=".SecondActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Im Second Activity"  />

    <Button
        android:id="@+id/click_second_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to go First"/>

</LinearLayout>

This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java

Read next

Modal Bottom Sheet in Androidx with Examples - Working with BottomSheet

A user can view the full Bottom Sheet by dragging the sheet up vertically. Bottom Sheets are a lesser known part of the Design support library.

Thu, 21 Apr 2022

Modal Bottom Sheet in Androidx with Examples - Working with BottomSheet

A user can view the full Bottom Sheet by dragging the sheet up vertically. Bottom Sheets are a lesser known part of the Design support library.
Thu, 21 Apr 2022

Using Custom and Downloadable Fonts in Android

To work with Custom Font, you need to install the latest version of Android Studio. A font resource defines a custom font that you can use in your app. Fonts can be individual font files or a collection of font files
Thu, 14 Apr 2022