Android Button Click New Activity Example

In this tutorial, you will learn how to implement a Button and an onclicklistener to open a new activity in your Android application. We will create a Button in an XML graphical layout and implement a button click listener together with an onclick method. The onclick method will start a new activity using an intent. So lets begin…

Create a new project in Eclipse, select File > New > Android Application Project. Fill in the details and name your project ButtonClickTutorial.

Application Name : ButtonClickTutorial

Project Name : ButtonClickTutorial

Package Name : com.androidbegin.buttonclicktutorial

Open your MainActivity.java and paste the following code.
MainActivity.java

package com.androidbegin.buttonclicktutorial;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);

		// Locate the button in activity_main.xml
		button = (Button) findViewById(R.id.MyButton);

		// Capture button clicks
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {

				// Start NewActivity.class
				Intent myIntent = new Intent(MainActivity.this,
						NewActivity.class);
				startActivity(myIntent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

We have implement a button click listener to capture button clicks and use an intent to start a new activity.

Next, create an XML file for your MainActivity graphical layout. Open your activity_main.xml in your res > layout folder and paste the following code.
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/MyButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button" />

</RelativeLayout>

Output:
Button Click Tutorial Activity XML

Next, create a new activity to show texts. Go to File > New > Class and name it NewActivity.java. Select your package named com.androidbegin.buttonclicktutorial and click Finish.
Open your NewActivity.java and paste the following code.
NewActivity.java

package com.androidbegin.buttonclicktutorial;

import android.os.Bundle;
import android.app.Activity;

public class NewActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from new_activity.xml
		setContentView(R.layout.new_activity);
	}
}

Next, create an XML file for your NewActivity graphical layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file new_activity.xml and paste the following code.
new_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:text="@string/welcome" />

</RelativeLayout>

Output:
Button Click Tutorial New Activity XML

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml

<resources>

<string name="app_name">Button Click Tutorial</string>
<string name="welcome">AndroidBegin - Android Button Click New Activity Example</string>
<string name="menu_settings">Settings</string>
<string name="Button">Button</string>

</resources>

In your AndroidManifest.xml, we need to declare an activity for NewActivity.java. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.buttonclicktutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NewActivity" >
        </activity>
    </application>

</manifest>

Output :

Button Click Tutorial ScreenShot

Source Code

[purchase_link id=”7806″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

Make 5 buttons in the XML file..Then in the New Activity set separately onClickListeners on 5 buttons and perform the required tasks.

Sukrit Mehta

Android Button Click New Activity Example

you can't, it's not possible

M Ased Ahmed

Android Button Click New Activity Example

nice blog thanks for sharing this informationhttp://www.punjablottery.inhttp://www.punjabstatelotterybumper.inhttp://www.lotterypunjab.in

varun

Android Button Click New Activity Example

if there are 5 buttons in the same activity, then how should I add action for each button in the same java file

deepika

Android Button Click New Activity Example