Course Content

Course Content

Description

Android Activity Data Passing Example

Now using all the components of UI which we studied, we will create activities. And in this we will pass some data from one activity to another activity.

We will see that how we can pass data from first activity to another activity, for that we will create an activity named 'DataPassing'.

Now inside that we will design two activities, to perform this thing we will design two layouts respective to both of the activities. First we'll open 'activity_first.xml'.

In which we have used one Linear layout, in which we have set its properties like height, width, padding etc... and in context we are showing that for which activitiy we are designing this layout.

After that we will do you identification for that,

android:id=""@+id/linearlayout1""

 

We have taken different type of text views here, whose basic properties we have set, like for first Textview we have set our text as ""First Activity"" and set all the colors we want.

As it is a linear layout so we have specified it before that orientation will be vertical. And after that we have taken three different kinds of EditText, as we want our user to enter their information.

And at last we have taken a button. On click event of this button we will pass our data.

Now in our second activity 'activity_second.xml' file, we have taken Relative layout, and set all the properties of it. And in context we have shown that it is for second activity.

We have taken a textview in which we have to show the relative position of one component to another component.

After that we have taken next textview in which we have shown text that appears when data is passed, it is ""Data passed successfully"".

Now we will see the code for first activity in 'FirstActivity.java' file, here we have done view identification for our two textViews, three editText and one button.

After that we have taken one OnClickListener,

btn1.setonClickListener(new View.onClickListener()

{

@Override

public void onClick(View v)

{

Intent intent1=new Intent(firstActivity.this,SecondActivity.class);

intent1.putExtra(""first_name"",edit1.getText().toString());

intent2.putExtra(""last_name"",edit1.getText().toString());

intent3.putExtra(""mobile"",edit1.getText().toString());

startActivityForResult(intent,1);

}

};

 

Now inner 'SecondActivity.xml', the intent which we are getting in second activity we are passing that here,

Intent intent2=getIntent();

 

To catch the particular data we will be using 'Bundle',

Bundle bundle=intent2.getExtras();

 

Now we will use two strings for first name and last name,

String first=bundle.getString(""first_name"");

String last=bundle.getString(""last_name"");

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere