Start another activity from one activity

  • 1. Create a new android project with two activities ActivityA and ActivityB and corresponding xml layouts activity_a.xml and activity_b.xml
  • 2. Add a button to the first activity’s layout
  • 3.Perform the action of loading ActivityB on button click

There are two ways we can add an action to a button in android
a. Assign a method to the button in the xml layout using the android:onClick attribute.
b. Set an OnClickListener to the button in the activity’s code.

1. Create a new android project in android studio.
In android studio go to File->New->New Project . Learn more
2. Add a button to the first activity’s layout.

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Activity B" />

Now activity_a.xml code looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    android:orientation="vertical"
    >
    <TextView
        android:layout_marginBottom="20dp"
        android:textSize="20sp"
        android:text="Activity A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Activity B" />
</LinearLayout>



And our activity_b.xml is below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_b"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:textSize="25sp"
        android:text="Activity B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

3. Perform the action of loading ActivityB on button click

Method a.Assign a method to the button in the xml layout using the android:onClick attribute:
Add android:onClick=”loadActivityB” to the Button element of activity_a.xml, now the Button element looks like

 <Button
        android:onClick="loadActivityB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Activity B" />

Here the value of the android:onClick attribute should be the name of the method that actually loads ActivityB. I have create the method named loadActivityB in ActivityA as show below:

This method must be a public method and must accept a View as the only parameter.
/*
*Loads ActivityB
*This method must be public
*Must accept only one parameter which is a View
*/
public void loadActivityB(View view)
    {
        Intent intent=new Intent(this,ActivityB.class);
        startActivity(intent);
    }

The complete ActivityA :

package com.droidmonk.basicactivity;

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

public class ActivityA extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
    }

    /*
    *Loads ActivityB
    *This method must be public
    *Must accept only one parameter which is a View
    */
    public void loadActivityB(View view)
    {
        /*
         * Create an intent to start ActivityB
         * */

        Intent intent=new Intent(this,ActivityB.class);
        startActivity(intent);
    }
}
Method b.Set an onClickListener to the button in activity’s code:
Add android:id=”button_load_b” to the Button element of activity_a.xml,
The android:onClick attribute is no more required. Now the Button element looks like

 <Button
        android:id="@+id/button_load_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Activity B" />

Now we have to access this Button from the ActivityA code and add the action to it.

 /*Access the Button from the layout using findViewById*/
        Button btnLoadActivityB= (Button) findViewById(R.id.button_load_b);

        /*set onClickListener*/
        btnLoadActivityB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                * Create an intent to start ActivityB
                * */
                Intent intent=new Intent(ActivityA.this,ActivityB.class);
                startActivity(intent);
            }
        });

The complete ActivityA :

package com.droidmonk.basicactivity;

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

public class ActivityA extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        /*Access the Button from the layout using findViewById*/
        Button btnLoadActivityB= (Button) findViewById(R.id.button_load_b);

        /*set onClickListener*/
        btnLoadActivityB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                * Create an intent to start ActivityB
                * */
                Intent intent=new Intent(ActivityA.this,ActivityB.class);
                startActivity(intent);
            }
        });
    }

}

Leave a Reply

Your email address will not be published.