Translate this page

Start Another Activity From Button in Android

In this Android tutorial, we will learn how to open a new Activity from a button click on android app. I hope you have already learned to make your first android app, try this link if you have not, Make your first android app

So let’s start

Create a new Button and TextView in your current activity_main.xml. Note the code in red line

The code for Button and TextView

        android:layout_centerHorizontal="true" />

        android:text="Activity one" />

// Note this code android:onClick="open2"

This onClick code is used to set action for the button, we will call the open2 from MainActivity java. Next we need create a new Activity, this will be the activity that will be opened When we click on button.

Creating a new activity

Create a new activity, an empty activity

Place your mouse pointer on the app folder and click to see the option menu, select new > Activity then select Empty activity

give name to this new activity as Main2Activity, now click finish. This is the activity that will be opened when we click on the button

open activity_main2.xml and add a new TextView

The TextView code

Add this code for TextView, we will see this in the second activity

        android:text="Activity 2 opened"
        android:layout_centerHorizontal="true" />

Adding code intent

Now open the Main2Activity java

Add this code like you see in this photo, in the oncreate method

public void open2 (View view){

 Intent intent = new Intent(MainActivity.this,Main2Activity.class);

The full code should be like this

package com.example.myapp.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
    public void open2 (View view){

        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
    } }

That’s it! our app is ready now. let’s test it on your device. So connect your phone to your computer with USB cable and run the app

Testing the App

now the app will be launched on your device, just click that button

The second activity is now launched when you clicked the button!

That’s it. For more advanced learning, you can subscribe to our YouTube channel.

Related Posts

Here is How to Get Host Domain from URL in Android Java

Sometimes, we might need extract domain name such as from the In android java, the code below can do it. It will output only the…

Android SoftKeyBoard Enter Key Handling

How to Handle Android Keyboard’s Enter Key to Support your Own Function By default, android soft keyboard’s enter key doesn’t support your own function even if you…

Test Intent and URL Schemes of Android Webview

URL Schemes Tester This page contains different URL Schemes that can be used to test various URL Schemes, it was created to test android webview, but other…

Realme XT Specifications, Launch Date, Price and Details

Realme xt details, launch date and reviews The Realme XT is an android smartphone with a massive 64 Mega pixels camera and qualcomm snapdragon 712 processor. Brand…

How to Copy a Webview Link in Android Java

Here is how to Copy current web address url in Android Java With the simple java code below, you can copy a link of current loaded website…

How to get Android App Version Programmatically in Java Android Studio

Here is how to get android app current version code programmatically in java android Sometimes, you will need get the current version number of your android app…

This Post Has One Comment

  1. Hi zid,

    Very good example. Please make some tutorial on android push notifications. Thanks

Leave a Reply

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