Python Tutorial

Friday, December 16, 2011

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



1 comment:

  1. Thank you for sharing your article. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.

    ReplyDelete