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.
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 :
res/layout/main.xml
– Represent screen 1res/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'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'm screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
2. Activities
Create two activity classes :
- AppActivity.java –> main.xml
- 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.
When above button is clicked, it will navigate to another screen App2Activity.java
(main2.xml).
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.
It did not work to me.Show error.Do i have to change something in my manifest?
Thank you for the tutorials, its very clear & helpful
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
Thank you very much for this tutorial.
excellent !!!
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!
Thanks for such a nice article Sir. Similar activity example with brief explanation can be found here:
http://android.programmerguru.com/android-activity/
this will work for two screens but i
am not able to connect more then two screen?
follow the same
Thanks for the tutorial, but ‘Broken Library’ when loading from the zip file??
Les
Nice tutorial for startup guys.
thanks.
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
Thank you very much for this tutorial.
It helped me a lot.
I have one doubt ,the first screen wait 5 second after move to next screen.
how do in android studio?
i have one doubt in acivity
A->B(Killed itself) -> C -> A(return value from c)
Awesome work. Thank you for help 🙂
Thanks u sir
how to perform action on multiple activities using single Button.
hello sir,
how to open a one activity in text on click and open a new activity
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.
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
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.
Very good information..thank you
i want to know how to XYZ Activity call on Main Activity by which all the data f XYX Activity show on Main
I am beginner to android,here it is getting error like Error:(25, 44) error: cannot find symbol variable button1,please provide information
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.
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)
THANK YOU. VERY NICE TUTORIAL.
This code worked perfectly! I had been struggling all day and using different codes but this one worked like a charm. Thank you!!
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
getting trouble with file uploading to google blobstore using struts 2.0