Hello guys, till now I give you the all basic knowledge of android that’s used to create your application. If you did not read any of that articles the click on this link:- https://smgplaza.com/category/android/. Now In this article, you can see how to go from one activity to second activity on button click event. So let’s see…..

Steps to create an application:-

steps to create an application

First, you need to open your existing project or if you create an application then follow the below instructions.

1. Open your Android Studio and click on start new Android Studio project.

android example 1

2. Now you need to select a project template whatever you want, I select Empty Activity for this article. After that click on the Next button.

android example 2

3. Now Configure your project with your application name and package name and then click on the finish button.

android example 3

now your project is created, that’s really simple right!

Code to create a button click event to go from one activity to the second activity:-

Now First open your activity_main.xml file and create any design whatever you want I created one Textview and Button like the below image.

android example 4

Here is my code to create a text view and button in the main activity. Before you create anything don’t forget to give id to any views. Because of that, we can call the button click event. The output of the below code is you can see on the above image.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="115dp"
        android:layout_centerInParent="true"
        android:text="Activity 1"
        android:textSize="50dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Next" />


</RelativeLayout>

Now you need to create second activity. But before that, you need to disable your “Compact Middle Packages“. If you don’t know how to disable it then follow below instruction.

First, go to the directory as you can see in the below image, and then you can see your package name in that directory. If your compact middle package option is enabled then you can see this type of structure.

android example 5

So If you want to disable that then click on the little gear icon (Gears icon). Now Uncheck / De-select the Compact Empty Middle Packages option.

android example 6

Now your package name is structure looks like this.

android example 7

After this, you need to create a second activity where you want to go from your main activity. So if you don’t know how to create the second activity then follow the below steps.

First go to the project panel at the left corner of your android studio.

Now go to the java folder and go to your package and click on the last directory of your package name where you can see your MainActivity.java file. Right-click on that directory and select New–>Activity–>Empty Activity Like Below image.

android example 8

Now give the name to that activity. I gave Activity2 name to that activity and click on the finish button.

Now open that activity XML file and design as you want in my activity I only created one TextView. My design for that second activity is given below.

android example 9

Code of second activity:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Activity2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_centerInParent="true"
        android:text="Activity 2"
        android:textSize="50dp" />


</RelativeLayout>

Now go to your MainActivity java file and call that button with id by using below code

Button next= findViewById(R.id.button1);

In above code you need to type your button. My button id is button1, you can see in blow image.

After that let’s create click event of that button so for that you need to type this code.

 next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              
            }
        });

Here next is variable that’s point to the button id. so using that we create a click event on above code. Now in the on Click method you can perform any operation. Using that you can perform anything on click of that button. so in this method we type below code.

Intent i = new Intent(MainActivity.this, Activity2.class);
                startActivity(i);

This code is used to going from one activity to another activity on the click event of the button. So here intent is a mechanism that allows users to coordinate the function of different activities.

Syntax of intent:-

Intent nameofintent = new Intent(firstactivity.this,secondactivity.class);
startActivity(nameofintent);

So this is how we can go from one activity to another activity. If any user wants to change the package name our application then click on below link.

Also Read:- How to change package name in Android Studio.

Other Articles :-

Follow us on below social media for latest update and if any one have any suggestion then you can contact us on any social media.

If you face any kind of problem then comment me comment box which given below this article. I try to give you the best article related to android so keep visiting Smgplaza and share this article with your friends. Thank you for your love and support.