In android to pass data between activity Intent is used.
You can use Intent.putExtras() in sender activity and
Intent.getExtras() in receiving activity.
Following Example explain intent in android.
For this example we create two xml files main.xml and receiver.xml and we create two corresponding java files AndroidIntentExampleActivity.java and ReceiverActivity.java.
Following are step by step explanation for this.
step 1 :- Create main.xml file. we create two TextView and one EditText and one Button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="@string/hello" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/enterdata"
android:text />
<EditText
android:id="@+id/enteryourdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Enter data"
android:padding="5dp" />
<Button
android:id="@+id/btnok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:text="Next" />
</LinearLayout>
step 2 :- Now corresponding to main.xml create one java file AndroidIntentExampleActivity.java which handles events.
public class AndroidIntentExampleActivity extends Activity implements
OnClickListener {
EditText txtdata;
Button btnok;
String datatopass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtdata = (EditText) findViewById(R.id.enteryourdata);
btnok = (Button) findViewById(R.id.btnok);
datatopass = txtdata.getText().toString();
btnok.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AndroidIntentExampleActivity.this,ReceiverActivity.class);
Bundle b = new Bundle();
b.putString("data", txtdata.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
Step 3 :- Now for receive data you need to create one xml file and one Java file. First create one xml file receiver.xml which display data passed from first activity.
receiver.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/displaytext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text" android:gravity="center"
android:textColor="@android:color/black"
android:textSize="20dp"
android:text />
</LinearLayout>
Here TextView display the text which is passed from first activity. Now for handling events create java file ReceiverActivity.java
step 4 :- Create ReceiverActivity.java
public class ReceiverActivity extends Activity {
TextView txtdata;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver);
txtdata = (TextView) findViewById(R.id.displaytext);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = String.valueOf(extras.getString("data"));
Toast.makeText(ReceiverActivity.this, "" + value, Toast.LENGTH_LONG)
.show();
txtdata.setText(value);
}
}
}
Following Example explain intent in android.
For this example we create two xml files main.xml and receiver.xml and we create two corresponding java files AndroidIntentExampleActivity.java and ReceiverActivity.java.
Following are step by step explanation for this.
step 1 :- Create main.xml file. we create two TextView and one EditText and one Button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="@string/hello" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/enterdata"
android:text />
<EditText
android:id="@+id/enteryourdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Enter data"
android:padding="5dp" />
<Button
android:id="@+id/btnok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:text="Next" />
</LinearLayout>
step 2 :- Now corresponding to main.xml create one java file AndroidIntentExampleActivity.java which handles events.
public class AndroidIntentExampleActivity extends Activity implements
OnClickListener {
EditText txtdata;
Button btnok;
String datatopass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtdata = (EditText) findViewById(R.id.enteryourdata);
btnok = (Button) findViewById(R.id.btnok);
datatopass = txtdata.getText().toString();
btnok.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AndroidIntentExampleActivity.this,ReceiverActivity.class);
Bundle b = new Bundle();
b.putString("data", txtdata.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
Step 3 :- Now for receive data you need to create one xml file and one Java file. First create one xml file receiver.xml which display data passed from first activity.
receiver.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/displaytext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text" android:gravity="center"
android:textColor="@android:color/black"
android:textSize="20dp"
android:text />
</LinearLayout>
Here TextView display the text which is passed from first activity. Now for handling events create java file ReceiverActivity.java
step 4 :- Create ReceiverActivity.java
public class ReceiverActivity extends Activity {
TextView txtdata;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver);
txtdata = (TextView) findViewById(R.id.displaytext);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = String.valueOf(extras.getString("data"));
Toast.makeText(ReceiverActivity.this, "" + value, Toast.LENGTH_LONG)
.show();
txtdata.setText(value);
}
}
}
No comments:
Post a Comment