Starting Another Activity

In this tutorial I will be discussing about switching between one activity to another activities.

First Method :-

  1. In Android Studio, from the res/layout directory, edit the content_my.xml file.
  2. Add the android:id="@+id/button" attribute to the <Button> element.
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
  3. In the java/akraj.snow.test directory, open the MainActivity.java file.
  4. add the <Button> method, use findViewById() to get the Button element.
    Button button = (Button) findViewById(R.id.button);
  5. Add OnClickListener method.
  6. button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
        }
    });

Second Method:-

  1. In Android Studio, from the res/layout directory, edit the content_my.xml file.
  2. Add the android:onClick attribute to the <Button> element.
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

    The android:onClick attribute’s value, "sendMessage", is the name of a method in your activity that the system calls when the user clicks the button.

  3. In the java/akraj.snow.test directory, open the MainActivity.java file.
  4. Within the MainActivity class, add the sendMessage() method stub shown below.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:

  • Be public
  • Have a void return value
  • Have a View as the only parameter (this will be the View that was clicked)

Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.

Build an Intent

In MyActivity.java, inside the sendMessage() method, create an Intent to start an activity called SecondActivity with the following code:

public void sendMessage(View view) {
  Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  startActivity(intent);
}

or

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent intent = new Intent(MainActivity.this, SecondActivity.class);
      startActivity(intent);
    } 
});

Create the Second Activity

  1. In Android Studio, in the java directory, select the package, akraj.snow.test, right-click, and select New > Activity > Blank Activity.
  2. In the Choose options window, fill in the activity details:
    • Activity Name: SecondActivity
    • Layout Name: activity_second
    • Title: Second Activity
    • Hierarchical Parent: akraj.snow.test.MainActivity
    • Package name: akraj.snow.test

    Click Finish.

  3. In your manifest file, AndroidManifest.xml, within the Application element, add the <activity> element for your SecondActivity class, as follows:
    <application ... >
        ...
        <activity
            android:name="akraj.snow.test.SecondActivity"
            android:label="@string/title_activity_second"
            android:parentActivityName="akraj.snow.test.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="akraj.snow.test.MainActivity" />
        </activity>
    </application>

Complete Code

content_main.xml (res/layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="akraj.snow.test.MainActivity"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />

</LinearLayout>

MainActivity.java (java/akraj.snow.test)

package akraj.snow.test;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

or

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }

    public void sendMessage(View view) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
    ...............
    .........

}

content_second.xml (res/layout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="akraj.snow.test.SecondActivity"
    tools:showIn="@layout/activity_second">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Activity"
        android:id="@+id/textView" />
</RelativeLayout>

SecondActivity.java (java/akraj.snow.test)

package akraj.snow.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

}

Next Lesson: Sending Data From One Activity to Another Activity

Question 1. What is Activity?

Answer: An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.

Learn more go to Android’s Activities Page…

Subscribe To Android Learning

Subscribe To Android Learning

Join our mailing list to receive the latest android tutorials from our site.

You have Successfully Subscribed!

Pin It on Pinterest

Shares
Share This