Using Buttons and Starting A New Activity in Android

In this tutorial you will learn how to start a new activity from an already opened activity also how to add a button to your activity and how to add functionality to the button such that when the button is pressed your app performs a function.

Adding a Button

Let’s start by adding a button to the layout file of your app’s activity.

Step 1: Go to the layout file of the activity to which you want to add the button to.

Step 2: Drag and drop the Button widget to the xml file in Design view

Add Button Drag And Drop

After Add Button

OR

Step 2: Add a button tag in the Text view inside the parent RelativeLayout.

The following code can be used to add the button to the top of the parent layout.
In the code below layout_width=“wrap_content” and layout_height=“wrap_content” tells the system to use as much space as required by the button while displaying the button.
You could try match_parent which will make the button take all the space of its parent.


<Button android:layout_width=“wrap_content" android:layout_height="wrap_content" android:text="Start Another Activity” android:id=“@+id/bStartActivtiy” />

Adding Button Tag

Step 3: Referencing the button in Java

Create a Button object and pass it Button widget using the function findViewById(), passing the id of the button you want the Button object to reference.
The following code shows this:

Button bStart = (Button) findViewById(R.id.bStartActivity);

Now that you have referenced the Button in java lets add some functionality so that when the button is clicked some function takes place.

Adding onClickListener to the button

You have to use setOnClickListener() method to add onClickListener to the button.
The following code shows how to use the setOnClickListener() method to add code that will be executed when the button is clicked:-


bStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // the code that is to be executed when button clicked comes here } });

onClick Method

Now that you have added the onClickListener to the button let’s add code that will start a new activity when clicked.

Creating A new activity And Starting It from another

Step 1: Go to File->New->Activity->EmptyActivity

NewActivity

Step 2: Give Activity Name , Its associated layout file name.

Naming Activity

Step 3: Create an object of type Intent

Create an object of type Intent in your previous activity’s onClick method that you added. Then assign it the new activity. Call the startActivity() method, passing it the intent object you created.
The code should look like this:-


bStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // the code that is to be executed when button clicked comes here Intent nIntent = null; try{ //assigning the new activity to the intent nIntent = new Intent(MainActivity.this,Class.forName(“com.anuraag.myapplication.NewActivity")); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { if(nIntent!=null) { startActivity(nIntent); } } } });

Code Activity Start

Now you can run the application to see that when you click the button you added, a new activity is opened.

ScreenShot 1

ScreenShot 2

This content has been helpful to you?

Thanks for contributing!

Yes No