How to Make a Button Open a New Activity – Android Studio Tutorial

How to Make a Button Open a New Activity – Android Studio Tutorial

This android mobile application tutorial shows how to open a new activity when the user clicks on “open new activity button” from another activity. So am going to create a method to open new activity using button onclick event, the method gets invoked/called when a user clicks a button. The code snippets have been made/coded using android studio and java.

SOURCE CODE

MainActivity.java

/**
 * How to Make a Button Open a New Activity – Android Studio Tutorial
 * https://mauricemuteti.info/how-to-make-a-button-open-a-new-activity-android-studio-tutorial/
 */
package com.example.howtoopennewactivityonbuttonclickandroidstudiomobileapplication;

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 {

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

        btnOpenActivity2 = (Button)findViewById(R.id.buttonOpenActivity2);

        //Button click event.
        btnOpenActivity2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //invoking Method for opening new activity.
                openNewAvtivityOnButtonClick();
            }
        });


    }

    /**
     * Method for opening new activity.
     */
    private void openNewAvtivityOnButtonClick() {
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
}

MainActivity2.java

/**
 * How to Make a Button Open a New Activity – Android Studio Tutorial
 * https://mauricemuteti.info/how-to-make-a-button-open-a-new-activity-android-studio-tutorial/
 */
package com.example.howtoopennewactivityonbuttonclickandroidstudiomobileapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity2 extends AppCompatActivity {

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

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:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonOpenActivity2"
        android:layout_width="204dp"
        android:layout_height="45dp"
        android:layout_marginStart="134dp"
        android:layout_marginLeft="134dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="73dp"
        android:layout_marginRight="73dp"
        android:text="Open Activity 2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</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">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="129dp"
        android:layout_marginLeft="129dp"
        android:layout_marginTop="355dp"
        android:layout_marginEnd="225dp"
        android:layout_marginRight="225dp"
        android:text="Activity 2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

VIDEO TUTORIAL

SCREENSHOTS

Leave a Reply

Your email address will not be published. Required fields are marked *