Tuesday, January 15, 2013

Android multiple screen same layout

It is very import use common layout for multiple screen if is possible. Now I show you a simple example of use single layout in multiple screen. And also manipulate them.



Screen A
Name:
Email:

Screen B
Address:
Location:



Let we need to construct this two screen. Here two screen is very similar, except some text. Let's do it by one layout




main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id ="@+id/labelA"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_x="0px"
android:layout_y="5px"
/>

<EditText android:id="@+id/edtInput"
android:layout_x="0px"
android:layout_y="40px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
/>

<TextView android:id ="@+id/labelB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email:"
android:layout_x="0px"
android:layout_y="110px"
/>

<EditText android:id="@+id/edtInputA"
android:layout_x="0px"
android:layout_y="150px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
/>

<Button android:id ="@+id/btnClick"
android:layout_width="180px"
android:layout_height="70px"
android:text="Open New Screen"
android:textSize="14px"
android:layout_x="0px"
android:layout_y="235px"
/>

</AbsoluteLayout>



MultipleScreenSameLayout.java
package com.example.MultipleScreenSameLayout;

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

public class MultipleScreenSameLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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(MultipleScreenSameLayout.this,
NewScreen.class);
startActivity(i);
}
});
}
}



NewScreen.java
package com.example.MultipleScreenSameLayout;


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

public class NewScreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView tvB=(TextView) findViewById(R.id.labelB);
tvB.setText("Address:");

TextView tvA=(TextView) findViewById(R.id.labelA);
tvA.setText("Location:");

Button b = (Button) findViewById(R.id.btnClick);
b.setText("Back");

b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
}


Don't fotget add both activity on manifest file



Output Screen


Screen A



Screen B