Hi friends
In this post we will learn how to send data(integer,long,string etc) from one activity to other.
Note: I hope you are familiar with using Buttons and EditTexts if not then go through this post Using Button and EditText in Android.
Some times while working with android we need to send some data from one Activity to other.
Here we will create a project which will have two activities "FirstActivity" and "Second Activity".
We send Name and Phone Number from FirstActivity to Second Activity and display the sent data.
Create a new project.
Donot forget to add SecondActivity in manifest.
Edit your main.xml file add two textviews, two edittexts, and a button.
It should look like (you can just copy the following xml code and paste in main.xml file)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:text="Phone Number"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="@+id/button"
android:text="OK"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
In this post we will learn how to send data(integer,long,string etc) from one activity to other.
Note: I hope you are familiar with using Buttons and EditTexts if not then go through this post Using Button and EditText in Android.
Some times while working with android we need to send some data from one Activity to other.
Here we will create a project which will have two activities "FirstActivity" and "Second Activity".
We send Name and Phone Number from FirstActivity to Second Activity and display the sent data.
Create a new project.
Donot forget to add SecondActivity in manifest.
Editing main.xml file
Edit your main.xml file add two textviews, two edittexts, and a button.
It should look like (you can just copy the following xml code and paste in main.xml file)
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:text="Phone Number"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="@+id/button"
android:text="OK"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Now open the FirstActivity class and write following
FirstActivity.java
public class FirstActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.button);
final EditText editTextName=(EditText)findViewById(R.id.name);
final EditText editTExtPhone=(EditText)findViewById(R.id.phone);
// add button onClick Listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=editTextName.getText().toString();
long phone=Long.parseLong(editTExtPhone.getText().toString());
// create a new intent
Intent intent =new Intent(getApplicationContext(),SecondActivity.class);
// put the name and phone(to be sent to other activity) in intent
intent.putExtra("PERSON_NAME", name);
intent.putExtra("PHONENUMBER", phone);
// start the second activity
startActivity(intent);
}
});
}
}
Edit your SecondActivity class
Point to Note(To Rememeber)
intent.putExtra("PERSON_NAME", name);
we have put the name in intent with "PERSON_NAME" tag, we will fetch name in second activity with same tag(see second activity). similarly we can send and fetch any type of data.
SecondActivity.java (Do not forget to declare this activity in manifest file)
public class SecondActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// extract the name and phone from intent
String name=getIntent().getStringExtra("PERSON_NAME");
long phoneNumber=getIntent().getLongExtra("PHONENUMBER", 0);
TextView tv=new TextView(this);
tv.setTextSize(20);
String str="NAME : "+name+"\nPhone Number : "+phoneNumber;
tv.setText(str);
setContentView(tv);
}
}
Now run the application, you will see the following
No comments :
Post a Comment