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



    

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



    

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



Button Click Tutorial
AndroidBegin - Android Button Click New Activity Example
Settings
Button

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



    

    
        
            
                

                
            
        
        
        
    

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