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
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Pingback: water ionizer loans()

  • Pingback: pay day loans()

  • Pingback: water ionizer()

  • Pingback: laan penge nu og her 18 aar()

  • Pingback: mobile porn movies()

  • Pingback: car parking()

  • Pingback: hurtigt lan penge nu()

  • Pingback: uk online casinos()

  • Pingback: emazinglights()

  • Pingback: Why Viber is Blocked in UAE()

  • Pingback: kangen water machine()

  • Pingback: kangen water()

  • Pingback: water ionizer()

  • Pingback: water ionizer()

  • Pingback: stream movies()

  • Pingback: watch movies online()

  • Pingback: watch free movies online()

  • Pingback: Blue Coaster33()

  • Fecxel

    very needed

    Thanks a lot

    i wanna bookmark it

  • nirmal

    i friends this is a very useful code for android beggiers

    here all the code given have very simple codes

    thanks ADMIN

  • Kumuda

    Hi,

    I am beginner to the Java programming and android …I am trying to create an app convertor which has 4-5 conversion money convertor, kgs to pound convertor..So, when I click on one conversion button it has to take me to the page pertaining to calculating the values….

    Actually before i had made an app which converts rupees to dollars..I am try use the same xml and java files here…I read tutorial and watched youtube and tried tht in my sdk but its not opening up anything so I do not which part is wrong or I made any mistakes in coding.. If any one out there can help it would be great.

    My code goes like this….MainActivity.java

    package com.example.converts;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;

    import android.view.View;
    import android.widget.Button;

    public class MainActivity extends Activity implements View.OnClickListener
    {
    Button button2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);
    button2=(Button)findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v){

    if(v.getId()==R.id.button2)

    {
    startActivity(new Intent(“com.example.converts.Main1″));
    }}
    });

    }
    }
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    }}

    My Main1 which I created has

    package com.example.converts;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.*;

    public class Main1 extends Activity implements OnClickListener {
    TextView dollars;
    TextView euros;
    RadioButton dtoe;
    RadioButton etod;
    Button calculate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

    dollars = (TextView)this.findViewById(R.id.dollars);
    euros = (TextView)this.findViewById(R.id.euros);

    dtoe = (RadioButton)this.findViewById(R.id.dtoe);

    etod = (RadioButton)this.findViewById(R.id.etod);

    calculate = (Button)this.findViewById(R.id.calculate);
    calculate.setOnClickListener(this);
    }

    public void onClick(View v) {
    if (dtoe.isChecked()) {
    convertDollarsToEuros();
    }
    if (etod.isChecked()) {
    convertEurosToDollars();
    }
    }

    protected void convertDollarsToEuros() {
    double val = Double .parseDouble(dollars.getText().toString());
    // in a real app, we’d get this off the ‘net
    euros.setText(Double .toString(val*0.67));
    }

    protected void convertEurosToDollars() {
    double val = Double .parseDouble(euros.getText().toString());
    // in a real app, we’d get this off the ‘net
    dollars.setText(Double.toString(val/0.67));

    }

    }

    My activity_mail.xml file has code

    android:typeface=”sans”
    android:textStyle=”bold”

    activity1.XML has the following code

    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=”.MainActivity” >

    So, please let me know what I am doing wrong..or what corrections I need to make to get this…

  • jawahar

    when am download codes and made existing project in my android project its working.but i made cut copy your codes its not working. can u tell me the reason please and what are the thinks i need to correct?

  • http://www.minivalai.blogspot.com Mathankumar V

    Hi,

    That was a useful explanation.

    I have a table in Excel. I should read the excel and create bar charts on screen.

    Statically, I am able to do this with excel file added as a resource to the package.

    But how do I read the Excel dynamically and I intend to update the Excel periodically.

    I can not use any databases.

    Instead of Excel, could the data be passed directly to all client devices using the application using XML?

    Thanks in advance,
    Mathan V.

  • Eddi

    Good to understand, works fine for me with 4 screen’s. Many thanks for this tutorial !!!!!

  • vinay

    thanks frd its very simple……………

  • Akari

    Thanks for the tutorial ! I’m a beginner and I find this lesson really interesting :) But I have a question… Do you usually declare your Views in the XML document or in the Java document ? Or do we have to do it on both ? What if I want to add Views to my layout dynamically ? Do I still need to define them on the XML document ?

  • Jafar

    Good one !. Simple and easy to understand..

  • Philip

    What is intent filter do? And you included it for main activity only. Can you explain a little more on this?

    Thank you for a great site.

  • cardin

    Needs more explanation about whats going on with the codes, especially about the 2 intents defined in the manifest file..

  • http://www.ezoteria.hu Maroy1

    Thanx for cool samples!
    Very helpful for beginners.
    Update pelase, I Can’t wait :-)

  • hello

    cool website it is very educational :) it is very nice how you strip down the app to the bare minimun for the explanation , it gets rid of all the noise

  • http://android.programmerguru.com Android Guru

    How to check whether Wi-Fi internet access is active or not in a device. Take a look at the post:
    http://android.programmerguru.com/android-check-wifi-internet-connection/