Tuesday, July 19, 2011

Android multiple screen example

Let we want constract an application which have two different screen and we want to move one screen to another scrren.



At first we need to constract this two screen individually, both screen should extends Activity.

After build two screen you add your new screen to AndroidManifest.xml file (first screen which extends Activity, it's automatically added to AndroidManifest.xml, but all new screen should be added individually).



//main.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 android:id ="@+id/btnClick"
android:layout_width="150px"
android:layout_height="50px"
android:text="Open New Screen"
/>

</LinearLayout>


//AndroidMultipleScreenEasy.java

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

public class AndroidMultipleScreenEasy extends Activity
{
MyNewScreen obB=new MyNewScreen();
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);

Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// here i call new screen;
Intent i = new Intent(AndroidMultipleScreenEasy.this, MyNewScreen.class);
startActivity(i);

}
});
}
}

//my_new_screen.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, press close for back to previous screen" />
<Button android:id="@+id/btnClick2" android:layout_width="100px"
android:layout_height="50px" android:text="Close" />

</LinearLayout>


//MyNewScreen.java

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

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

public void setOb( AndroidMultipleScreenEasy obA){
this.ob=obA;
}
}



Add this line to your AndroidManifest.xml file.



//AndroidManifest.xml
<activity android:name=".MyNewScreen" android:label="MyNewScreenLabel"> </activity>



output:



Screen 1


Now you can goto new screen by clicking Open New Screen button.


Image not found


Screen 2


Now you can goto previous screen by clicking Close button.


Image not found