Thursday, January 31, 2008

Opening New Screen in Android


An Android application, at most, contains more than one screen. But, how we can open a new screen after clicking a button, choosing a menu, or other? It is not too difficult to do in Android.

There are a few step to do, to open a new screen in android. You have to create an activity class, an xml layout, update AndroidManifest to know your second/third .. activity class, and put a code to call the new activity / screen.

An Activity class can be created with extending from Activity, ListActivity or other Activity class. Each Activity Class should have an xml layout as a view screen as a representation of the Activity Class.

Android Manifest should be updated to know the new activity / screen. so, when requested to show the new screen, it knows which class to execute.

At last you should put a piece of code to first screen, to show the new screen. Below is an example to open a new screen:

//screen1.java


package test.android;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;

public class Screen1 extends Activity
{
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.screen1);
      Button b = (Button) findViewById(R.id.btnClick);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
         Intent i = new Intent(screen1.this, screen2.class);
         startActivity(i);
         }
      });
   }
}

//screen1.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
>
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="You are in the first Screen"
/>
<Button
   id ="@+id/btnClick"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Open New Screen"
/>

</LinearLayout>

//screen2.java


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

public class Screen2 extends Activity
{
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.screen2);
      Button b = (Button) findViewById(R.id.btnClick2);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
         setResult(RESULT_OK);
         finish();
         }
      });
   }
}

//screen2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
>
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="You are in the New Screen"
/>
<Button
   id ="@+id/btnClick2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Close"
/>

</LinearLayout>

//AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="test.android">
   <application android:icon="@drawable/icon">
      <activity class=".Screen1" android:label="Screen 1">
         <intent-filter>
         <action android:value="android.intent.action.MAIN" />
         <category android:value="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity class=".Screen2" android:label="Screen 2">
      </activity>
   </application>
</manifest>





ok, Have a nice try!

Do you want to learn more ?
Learning with sample code ?
Learning by Doing ?
Just Visit http://learncodes.googlepages.com/
and there is Android UI Design at there.
You can download code almost the same as this at there too

18 comments:

andcoder said...

Thank you very much ...
I was desperately searching for this page..really good example

Unknown said...

Thank you very much, I am a beginner, searching a lot and at last find your example.

regards.

Anonymous said...

THANK YOU SO MUCH, no where on 'net does this tutorial exist.. simple enough yet no one has time to explain it to a beginner...

Much appreciated

Unknown said...

Thank you for posting this. As the others said, it is hard to find this (relatively simple) material presented as clearly as you have. I had to make a couple changes, possibly due to using a newer SDK, or maybe just my system configuration - but I thought I'd share . . .
In the XMLs:
id ="@+id/btnClick2"
becomes
android:id ="@+id/btnClick2"

in the manifest:
class=".Screen1"
becomes
android:name=".Screen1"

Thanks again . . .

Clive Jefferies said...

Thanks very much. This tutorial was really helpful. Its a shame the Google documentation cannot be so concise.

Kenneth said...

Thanks! You are a rock!

Anonymous said...

Excellent, the changes put for by another user helped to get me up and going. Folks writing books for Android, put this example in your book and give this gent a head nod. Crazy how the book I'm just finishing does not even explain this, nor how to setup the Eclipse IDE. They need to take a clue from this to-the-point post. THANK YOU FOR POSTING!

Unknown said...
This comment has been removed by the author.
Unknown said...

I am getting 3 errors on the lines:

setContentView(R.layout.Screen2);
Button b = (Button) findViewById(R.id.btnClick2);
b.setOnClickListener(new View.OnClickListener()

please give me suggestions.

John Denon said...

Thank you! I have been looking for this solution for hours!

Sasil John Gabriel said...

Hi all,
i have created an activity,
onfling i want to load new values into it .
can anyone plz help me

triedandtested said...

Excellent. Solved my problem...

Anonymous said...

Excellent example ... be sure to verify the caps on Screen1(screen1) and Screen2(screen2) .. other than that - I also have to as android:id as someone stated .. but it works perfect. very straight forward .. and definitely something hard to find online .. thank you for putting it out there. If I found this before the weekend i might not have spent $45 on an android book that didn't help.

Nivedita said...

I am new to Android application,and your tutorial helps me a lot.

Thanks

Nivedita said...

I am new to Android application,and your tutorial helps me a lot.

Thanks

Nivedita said...

I am new to Android application,and your tutorial helped me a lot.

Thanks

Unknown said...

Awesome Tutorial...thank you very much..it really helped me a lot..Hope you post many tutorials like this

Abiral said...

Awesome, great job. Thankx a lot