Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
104 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
anand jagtap
11 years ago

I have one activity say UserList and second one is chatwindow.
UserListActivity contains events which are executed on threads to update list of available users and take care send recieve message.
when user clicks on user in list new window should open in tab.
when message recieved from server it should be shown in chat window.
I could i achive this?
How i can desing the screen and manage the single user activity can handle multiple chatwindow activities of same type.
Any help would be highly appriciated.

skakavac
12 years ago

It did not work to me.Show error.Do i have to change something in my manifest?

Mithun
6 years ago

Thank you for the tutorials, its very clear & helpful

Jeff
8 years ago

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

kruchy
11 years ago

Thank you very much for this tutorial.

vibhor
11 years ago

excellent !!!

mirenibg
11 years ago

Amazing tutorial, pretty simple and nicely explained. It works perfect except i couldn’t run the sourcecode.zip properly, but writing it by myself helped me understand it, because I’ve been wondering why you defined and object button in App2Activity.java. Nevermind, thanks again, the best android tutorial for activity!

Udhay
11 years ago

Thanks for such a nice article Sir. Similar activity example with brief explanation can be found here:
http://android.programmerguru.com/android-activity/

radhika
11 years ago

this will work for two screens but i
am not able to connect more then two screen?

balwinder singh
11 years ago
Reply to  radhika

follow the same

Les
12 years ago

Thanks for the tutorial, but ‘Broken Library’ when loading from the zip file??

Les

techieSambath
12 years ago

Nice tutorial for startup guys.

thanks.

John
12 years ago

Very, very useful to a beginner like me who is struggling with people assuming I already know more than I do. Keep it up buddy!

Thanks,

John

Gem
12 years ago

Thank you very much for this tutorial.
It helped me a lot.

Sundharam S
1 year ago

I have one doubt ,the first screen wait 5 second after move to next screen.
how do in android studio?

Vishnu kumar G R
4 years ago

i have one doubt in acivity
A->B(Killed itself) -> C -> A(return value from c)

Anagha
4 years ago

Awesome work. Thank you for help 🙂

Ashok
5 years ago

Thanks u sir

Shashank Saini
6 years ago

how to perform action on multiple activities using single Button.

KIRANKUMAR
6 years ago

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

Dipen
6 years ago

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
7 years ago

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
6 years ago
Reply to  Mohit Asher

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
7 years ago

Very good information..thank you

Jay Kishan
7 years ago

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

rai
7 years ago

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
8 years ago

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
8 years ago

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
8 years ago

THANK YOU. VERY NICE TUTORIAL.

PinkNGreen
8 years ago

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
8 years ago

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
8 years ago

getting trouble with file uploading to google blobstore using struts 2.0