How to pass data from one activity to another in Android?

Lets suppose you have one EditText and one button in one activity and one text view in another activity.

On clicking the button, text in Edit Text of activity one should become lable/ text of text view in another activity.

For this:

Define onClick attribute of button with function to be called as value of the attribute.

android:onClick = login

 where login is the name of function that is called when button is clicked.

 Now define Intent object inside this login() function.

 Intent myIntent = new Intent( this, DashBoard.class);

Here DashBoard is the source java file of second activity.

To pass the data, first you need to fetch the data of EditText.

 EditText mEditText= (EditText)findViewById(;

 It will fetch the object of that EditText.

 String str = mEditText.getText().toString();

 Now text of EditText (id: editText1) is in string variable str.

 Now pass the data by calling putExtra() method through Intent object created above and start the second activity.

 myIntent.putExtra("myExtra", str);

Here myExtra is the name of string being passed and str is variable containing the actual string.

 In the second activity that you named DashBoard, you need to get the Intent and set string of previous activity as text of TextView of second activity.


Intent myIntent = getIntent();
if (myIntent.hasExtra("myExtra")){                                TextView mText = (TextView)findViewById(;
mText.setText("Welcome "+myIntent.getStringExtra("myExtra")+"!"); }

 At this point, you are still not done!

 You need to add second activity name in AndroidManifest.xml file to make operating system know about the existence of second activity.



 And you are done. 🙂


About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s