Android activity – from one screen to another screen

In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens, for example, one activity to display a list of the application settings, another activity to display the application status.

Note
Refer to this official Android activity article to understand more about Android activity.

In this tutorial, we show you how to interact with activity, when a button is clicked, navigate from current screen (current activity) to another screen (another activity).

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. XML Layouts

Create following two XML layout files in “res/layout/” folder :

  1. res/layout/main.xml – Represent screen 1
  2. res/layout/main2.xml – Represent screen 2

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m screen 1 (main.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me to another screen" />

</LinearLayout>

File : res/layout/main2.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m screen 2 (main2.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

2. Activities

Create two activity classes :

  1. AppActivity.java –> main.xml
  2. App2Activity.java –> main2.xml

To navigate from one screen to another screen, use following code :


    Intent intent = new Intent(context, anotherActivity.class);
    startActivity(intent); 

File : AppActivity.java


package com.mkyong.android;

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

public class AppActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addListenerOnButton();
	}

	public void addListenerOnButton() {

		final Context context = this;

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

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			    Intent intent = new Intent(context, App2Activity.class);
                            startActivity(intent);   

			}

		});

	}

}

File : App2Activity.java


package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class App2Activity extends Activity {

	Button button;

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

}

3. AndroidManifest.xml

Declares above two activity classes in AndroidManifest.xml.

File : AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".App2Activity" >
        </activity>
    </application>

</manifest>

4. Demo

Run application.

AppActivity.java (main.xml) screen is display.

android activity demo1

When above button is clicked, it will navigate to another screen App2Activity.java (main2.xml).

android activity demo2

Download Source Code

References

  1. Android activities example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Mithun
Guest
Mithun

Thank you for the tutorials, its very clear & helpful

Jeff
Guest
Jeff

Hi, thanks for the tutorial, im new to android programming. How can i kill/finish/destroy a layout login. Just like facebook app prevents you from login all the time

KIRANKUMAR
Guest
KIRANKUMAR

hello sir,
how to open a one activity in text on click and open a new activity

Dipen
Guest
Dipen

Can you help me this please. On a button click i want to perform activities one after other but it performs all activities simultaneously. For example I want to display next layout after completion of animation.But it directly displays layout.

Mohit Asher
Guest
Mohit Asher

hi.. i am a beginner. i want to create 20 pages. i have a button, 3 radio buttons and a textview in all the pages i want to create. do i need an activity for all pages or can i create this in a single activity as layout is same

Michel GilliƩron
Guest
Michel GilliƩron

Sure you can use only one activity for your 20 pages. When you create an intent to start the activity, you can use method putExtra on this intent to pass some data to your activity. Finally, in the activity, use getIntent() to get activity’s intent and use on it getExtra methods to obtain your data (for e.g. method getStringExtra if the expected data is a String value. You could then apply these values for objects of activity’s layout.

Muyassar Abdullah
Guest
Muyassar Abdullah

Very good information..thank you

Jay Kishan
Guest
Jay Kishan

i want to know how to XYZ Activity call on Main Activity by which all the data f XYX Activity show on Main

rai
Guest
rai

I am beginner to android,here it is getting error like Error:(25, 44) error: cannot find symbol variable button1,please provide information

milan chhatralia
Guest
milan chhatralia

Hey mkyong
I am very much Thank full to you about above showed examples.
My query is how to get alarm on full battery charge?
Please give reply as fast as possible.

shubham singh
Guest
shubham singh

from moving from main activity to activity 1 its working but when moviong from activity 1 to activity2 its not working and promting a message(unfortunately application has been stoped)

Rana Saha
Guest
Rana Saha

THANK YOU. VERY NICE TUTORIAL.

PinkNGreen
Guest
PinkNGreen

This code worked perfectly! I had been struggling all day and using different codes but this one worked like a charm. Thank you!!

Philip Jeffrey Trowe
Guest
Philip Jeffrey Trowe

If you would like to learn how to use the following objects to write an Android application that displays a vertically upward scrolling Rainbow of colours in a FREE video, then click the link at the end of this comment:

. Activity
. LinearLayout
. View
. Canvas
. Paint
. ArrayList

http://androidprogrammeringcorner.blogspot.com/2015/06/pak-longs-android-programming-corner_24.html

Raman
Guest
Raman

getting trouble with file uploading to google blobstore using struts 2.0

Priyan
Guest
Priyan

Hello ,I have been watching your blog for various solutions , everything is best and clear solution.Iam a beginner for android devlopment , through this page i learnt activity and below are my questions
Please clarify
1.I have so many layouts so for each layouts should i create a activity.java file ?
2.Can we parameterize somehow?

Thanks

shiva
Guest
shiva

hello sir! i have a doubt will you please help me

HOW TO LAUNCH ONE APK FROM ANOTHER APK USING INTENT

Michael
Guest
Michael

very nice, your smart developer

Rob
Guest
Rob

Simple, You are one great teacher MKyong!
Thank You! I hope to be as good as you one day.

Rejuvenated Horizons
Guest
Rejuvenated Horizons

when I import R, then it shifts to main ..same error but it is with main this time… any tips?

Rejuvenated Horizons
Guest
Rejuvenated Horizons

this stupid R error… how can I get rid of it?

samozain
Guest
samozain

Hello
Thank you so much for this tutorial, it made me code 3 button link but the 4th worked.

samozain
Guest
samozain

Th

Android Guru
Guest
Android Guru

Nice Tutorials!
You can also Android tutorials for Beginners at http://android.programmerguru.com

don
Guest
don

thankyou sir.

ravi
Guest
ravi

tahnk you and superb

Duke
Guest
Duke

Couldnt be any simpler and clearer. Thanks Yong!

Mike
Guest
Mike

Excellent, thank you!

Fecxel
Guest
Fecxel

very needed

Thanks a lot

i wanna bookmark it

dev
Guest
dev
nirmal
Guest
nirmal

i friends this is a very useful code for android beggiers

here all the code given have very simple codes

thanks ADMIN