Python Tutorial

Friday, December 16, 2011

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


Android multiple screen passing data through intent

For passing data one screen to another screen on android you need to pass it through the intent. This example very similar to my previous example, only extra feature is passing data one screen to another screen. Let's go to the example:

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="180px"
   android:layout_height="70px"
   android:text="Open New Screen"
   android:textSize="14px"
/>

</LinearLayout>



AndroidMultipleScreenEasy.java
package com.example.AndroidMultipleScreenEasy;

import java.util.ArrayList;

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) {
    
    ArrayList tdata = new ArrayList();
    tdata.add("This text from first screen");
    // here i call new screen, and pass the data;
    Intent i = new Intent(AndroidMultipleScreenEasy.this,
      MyNewScreen.class);
    i.putExtra("Screen1Data", tdata);
    
    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 Back for back to previous screen" />
 <Button android:id="@+id/btnClick2" android:layout_width="100px"
  android:layout_height="50px" android:text="Back" />
 <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</LinearLayout>




MyNewScreen.java
package com.example.AndroidMultipleScreenEasy;

import java.util.ArrayList;

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

public class MyNewScreen extends Activity {
 AndroidMultipleScreenEasy ob;
 TextView tv;
 
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.my_new_screen);
  
  tv = (TextView) findViewById(R.id.myTextViev);
  Intent sender = getIntent();
  
  ArrayList tdata = (ArrayList) sender.getExtras().getCharSequenceArrayList("Screen1Data");
  tv.setText(tdata.get(0));
  
  Button b = (Button) findViewById(R.id.btnClick2);
  b.setOnClickListener(new View.OnClickListener() {
   public void onClick(View arg0) {
    setResult(RESULT_OK);
    finish();
   }
  });
 }
}



Output Screen:


First screen


Second screen