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

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="144dp"
        android:onClick="open2"
        android:text="Button1"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textSize="30dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="129dp"
        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

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Activity 2 opened"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

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);
 startActivity(intent);
}}

The full code should be like this

package com.example.myapp.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void open2 (View view){

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

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 www.google.com from the www.google.com/search=searchQuery. 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 *