timthumb

Android Switching Between Activities – Example

In this Tutorial we will discuss switching between activities in Android. We are going to use Android Intent and OnClickListener in this tutorial. We are going to create two Activities “Activity1″ and “Activity2″ and switch between them on Button Click.

Initial Setup:

For Initial Setup of SDK and AVD follow this tutorial Setup Android SDK /IDE /AVD in Windows and Ubuntu

Creating New Project:

Create new project in eclipse by selecting File -> New -> Android Application Project. Give a Application name, Project name and Package name. Here my package name is learn2crack.switchactivity. In the next step enter the Activity name and Layout name. Here I created the main Activity as Activity1 and main Layout as layout1.

switching between multiple activities

Layout 1- layout1.xml

Layout 1 has a Text View which shows the current activity and a button to switch to Activity 2.

layout1.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Activity 1 Layout" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Switch to Activity 2" />

</RelativeLayout>

Creating Layout 2- layout2.xml

Create new layout by File -> New -> Android XML File . Layout 2 has a Text View which shows the current activity and a button to switch to Activity 1.

layout2.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Activity 2 Layout" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Switch Back to Activity 1" />

</RelativeLayout>

 

Activity 1 – Activity1.java

Here we use OnClickListener to identify the button click and Intent to switch to Activity2.

Activity1.java

package learn2crack.switchactivity;

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

public class Activity1 extends Activity {

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

		final Button switchact =(Button)findViewById(R.id.btn1);
		switchact.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent act2 = new Intent(view.getContext(),Activity2.class);
				startActivity(act2);

			}
		});
}}

 

AndroidManifest.xml

This is used to declare the Activity and the Permissions required by the application.
Add the block to define Activity 2.

<activity
android:name="Activity2"
android:label="@string/app_name">
</activity>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="learn2crack.switchactivity"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="Activity2"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

 

Activity 2 – Acivity2.java

Here we use OnClickListener to identify the button click and Intent to switch back to Activity1.

Activity2.java

package learn2crack.switchactivity;

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

public class Activity2 extends Activity {

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

		final Button switchact =(Button)findViewById(R.id.btn2);
		switchact.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent act1 = new Intent(view.getContext(),Activity1.class);
				startActivity(act1);

			}
		});
}}

 

Execution:

Finally run the project in emulator and test.

Screenshot.

switch between multiple activities

Activity 1

Any questions comment here

Enjoy :)

Raj Amal W
Raj Amal is a Co-founder and Android Developer of Learn2crack.com. He Loves to work with Computers, Smartphones and Digital Gadgets.
Raj Amal W
  • Hbissle

    Hi,
    I’ve tried the tut but it references Activity1.class / Activity2.class in your java files and my code has an error because I don’t have them in this project can you please advise where to get them. Or how (and in what folder) to create them?

    Thank you.

    HR

    • http://128.199.224.11/ Raj Amal

      You must create the JAVA classes in “src” folder.

  • A Toggler WannaBe

    The above code doesn’t “switch between activities”. It just creates instance after instance of the activities. I’m trying to switch between activities.

  • carterson2

    Raj, Maybe if you included a zip file, people could try easier. Thanks for posting!

  • Francis Lambeets

    The code works fine, even using an EditText field instead of a button. However, Toggler WannaBe is right when he says the code doesn’t really switch, it indeed creates instances of both activities one upon the other. Is there a way to close the previous activity after each switch?