1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Drain Droids

Android Studio For Beginners.Mobile Apps.Android Games.

Wednesday, December 26, 2018

How to Create Button Click to Open Next Activity in Android Studio



In this short tutorial, I will show you how to make a button on the mainActivity page, clicking on the button goes to next Activity page called second activity.

You will often use this process as you moving from one activity page to another activity page. This guide is short and simple, easy to grasp.
Taking a look at the image bellow, you will see two Activity Pages, One is the MainActivity and the Next or Second Page is SecondActivity. So our Aim is to click a Button on the Main Page to open up the page on the Second Activity.



Step 1
On The Main Activity XML, first thing you want to do is to create a Button on the Main Activity. On the Palette, simply drag your button to the main page XML. You can watch the short Video to see how the button is drag to the main page.

Step 2

                Let’s name the onClick as openNextActivity.To do this, click on the button, the on the right attribute locate onClick and name it openNextActivity

Step 3
Now let Create Second Activity called second

Step 4

I always like to create new activity from JAVA containing my package name, the reason is, and it will automatically create Java and XML activity for the second Activity so you don’t have to create it individually.

So to do this, click on Java, Right click your project, click new, click Activity, and click empty activity. Here name it second. The process is in the video.

Now, that you have created the second activity, put some text on it using TextView   

You can also waatch the Video here 

                        

Step 5

Next go the MainActivit.Java paste bellow code on the onCreate

}
    public void openNextActivity (View view) {
        Intent intent=new Intent(MainActivity.this,second.class);
        startActivity(intent);

You notice the openNextActivity and the Second?
Remember we set OpenNextActivity on onClick
Remember we created next activity and named it second?

If you have followed this process correctly, run your project. Click on the button it will take you to next page called second.