1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Sending Data Between Screens in Android App Development

Sending Data Between Screens in Android App Development

Sending Data from Activity to Activity - Part 1
1h 27m

This course explores how to send data from one screen to another in Android applications.

Learning Objectives

  • Learn how to send data from one activity to another activity
  • Learn how to send data from activity to fragment
  • Learn how to send data from fragment to activity
  • Learn how to send data from one fragment to another fragment

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.


To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.


Alright. Hello Friends. Everyone included. Here we are in the next section. And this is going to be interesting because we're going to learn how to send data from one screen to another in Android applications. And guess what?

We're going to deal with all these transfers in just four steps. The first one is data transfer from one activity to another. So, we'll learn how to send data from one activity to another activity. The second step, we're going to learn how to send data from activity to fragment. What? Is that possible? Yes. Third section, we're going to learn how to send data from fragment to activity. And then the last step is we're going to learn how to send data from one fragment to another fragment, which Natch you probably were already doing, right?  So, sometimes a part of developing an application, we're going to have to transfer data that is taken from the user or from some other part of the application. That's why this section is  so vital, alright? So, in this section, we're going to learn step by step what we're going to do.

First step, sending data from one activity to another, alright? So, in this section, we're going to learn how to send data to another activity. Then we're going to learn how to get that data that was sent from the first activity. And then finally, we'll learn how to write these data that have been taken into Android components.

So, every step has a breakdown of steps. But no more talk. Let's get right to Android Studio, right? So, here you can see I've created a new project for this video. So, you already know how to create a project. I'm going to leave that up to you. So, I'll show you first the finished version of this project. In the application, the user is going to enter a name and email address and a phone number. Also see here we have used a different EditText structure. So, when you start to enter data into EditText, the Int does not disappear and moves toward the top of the text field.

In addition, we can specify the number of characters to be entered in each EditText component. And that if we exceed the character limit, the color of the text changes. That's cool. Additionally to that, an icon appears automatically to delete the data that we wrote in the EditTexts. So, by using this, we can delete the data that we've written. Then after clicking the 'Sign up' button, a new activity will open, all right? So, you can consider this to be a login page. Users have to enter some information on the login page and then they can proceed further, right?

So, now we'll need to get all this entered data and then send them to another page, right? First, I'll write a name in the name section, then I'll write an email address, and I'm going to write a phone number. All right. So, I'm going to click the 'Sign up' button and then you can see three written TextViews that the user entered. I made some additions here to the written text. For example, we added a "Hello" before the name and we've also added "your email is" before the email address. And similarly, we've added "your phone number is" before the phone number. Makes it more human, don't you think?

Okay. So, obviously, it's a simple application but there's some great components in here and obviously some great fragments. So, let's get started. So, first up, I want to delete the ConstraintLayout, and instead of it, I will use the LinearLayout. Also the OrientationLayout is going to be vertical, and the gravity property can be center_horizontal. Now, we can add three EditText of the LinearLayout, but first, this time I'm not going to use the classic EditText component. Here, I'm going to use the TextInputEditText. So, for this, I'll need to drag and drop the TextInput layout component from the palette and right in here to the component tree. As you can see, the TextInputEditText component is under the TextInputLayout.

Cool. So, I'm going to add two more TextInputLayout. And as you can see, other components are not visible on the screen. Well, the height of the three components is match_parent. So, that means I'll need to select these three components and change the layout height property to wrap_content. Also the width of the three components can be 300dp. Okay,  so now all three components are visible. So, why don't we organize the components a little bit? So, let's edit the TextInputayout components first, and then I will edit the EditText components in each layout.

Now, for this reason, if you select the components, you will edit from the component tree; you're not going to get confused with the different features. I do want you to pay attention to this though, right? So, edit from the component tree. So, now I'll just choose the first TextInputLayout. And I'm making the margin top value to be 50dp. I'll choose the second and the third TextInputayout, and I'll make the margin top values of these 20dp. All right,  so now we want to modify the other properties of these TextInputayouts from the XML file. So, I'll click on the 'Split' option. Now you can see the XML and the Design section together. All right,  so that way you can follow the features that we added way more easily.

Okay, so first I want to edit the first TextInputayout. If you remember, under each EditText, there was a counter that counted the characters that we entered. So, let's have that first. I want to the use app:counterEnabled feature for this. Property will be true. Now, we've got to determine the number of characters here. So, the first EditText is for the username. Let's just say 10 characters will be enough. So, for this, I'll use the app:counterMaxLength feature, and I'll type in 10.

Now, let's create the delete icon that appears when the data in the EditText exceeds 10, right?  So, I'll use the app:endIconMode feature for this. I'll write "clear_text". And now let's create the helper text under each EditText. I'm creating the app:helperText feature for this. And of course, you can write any expression you want to, but I'll just write in "Write your name" for the first EditText. And we can also give a color to it, for example, the color that text is red. I don't attract more attention, right? So, I'm using the helperTextTextColor property. Of course, we didn't create any color code for the red color and the color.xml file. We can use the ready-made colors available in Android library. So, here I'll write @android:color/ holo_red_dark. And then finally, I'll just use a special style to find in the material design library for the background shape of the layout. And I'm going to use this style attribute here.

Next I want to type @style/ and select the style "Widget.Material Components.TextInputLayout.OutlinedBox". Okay, my friends, we have defined the properties of the first TextInputayout. Of course, since these features will be common to the other two TextInputayouts, I'll just copy the codes here and paste them between the tags of the second layout. And guess what?

I can also paste it in for the third layout. Now, here, it will be sufficient to change only the helper texts as well as number of characters. So, the second layout helper text could be "write your email". The number of characters let's make it 15. Helper text of the third layout can be "Write your phone," and we'll make the number of characters 20. That way, we've defined the properties of the TextInpuLayouts. Now, let's edit the EditTexts in each layout, okay?  So, the first TextInputEditText will be for username. So, the inputType property can be textPersonName. We can also write the name to the Int property and the text size can also be 20sp. So, finally let's give an ID to this EditText. ID can be editTextName.

Okay, now let's create similar properties for the second EditText. So, the ID is going to be editTextEmail. Text input property will be the textEmailAddress. And I'm writing the email for the Int property. And text size can also be 20. And finally, I'll create similar properties for the third EditText. Let the ID be edit editTextphone. Text input property will be phone. So, I'm typing "Phone Number" for the Int property. And we will make the text size 20 as well. Okay, my friend. So, that determines all the necessary features for these EditTexts. Now, let's add a button to the design area. Now, the width of this button can be wrap_content. And I'm going to change the text of this button to Sign up. All right, the margin top value of the button can be 50. And lastly, we can give an ID to the button.

So, the ID can buttonSignUp. All right. So, that's the design all finished. Let's go over to the Kotlin class. First up, I'm going to define the EditText components. lateinit var_etName :. After the colon, we should see TextInputEditText. Not only the EditText, right? Be careful right here because you don't want your input EditText to behave like a normal EditText, do you? So, let's continue lateinit var etEmail : TextInputEditText. lateinit var etPhone : TextInputEditText. And for the button, lateinit var signUp : Button. And then inside the onCreate method I will match components with their IDs. etName = findViewByID.R.Id.ediTextName. etEmail = findViewByID.R.Id.ediTextEmail. etPhone = findViewByID.R.Id.ediTextPhone.

All right. So, lastly, signUp = findViewByID.R.Id.buttonSignUp. And now let's just come in here and add a ClickListener to the button. So, inside curly brackets, let's think about it. First, I got to get the data that the user enters, then I got to assign them to the string and integer container. All right. So, for this I'll just write in here, val name = etName.text.toString. Just like that, I will get the other data as well. So, val email = etEmail.text.toString. val phone = etPhonetext.toString.

Now, as a difference, here you will be able to get the phone data as long. So, I will change the form just by writing toLong, you understand? So, in fact, instead of getting a long value, we could have gotten the information as a string in the first place and just written it as it is. I did that on purpose because I want to show you how to send long data, right? So, here I just made a long transformation. Also I prefer the long data type instead of the integer just because the phone number can be long. You've got to consider all the different country codes from everywhere in the world. Yes, you do.

All right. So, now we can send data. So, we're going to use Intent as our sending operation. So, at first I'm going to create an object in the Intent class. Here the intent class gets two parameters. First one is name the activity in which the intent will work. It's going to be enough here to just write keyword this because you notice? Here, this keyword represents main activity. Besides that, you can also write this@MainActivity here. So, the choice is up to you. The second parameter will be the activity that we want to open. That's why I am writing in here SecondActivity::class.java.

All right. So, we get a warning. Why is that? Because we don't actually have an activity called second activity. Yet, Android Studio doesn't know that. Thanks for the warning. We're going to create that activity soon. So, now we can pass to the data sending section. So, here I'm going to write Intent.putExtra. Now, we use the putExtra() method so that we can send data with Intent, right? So, it's thanks to this method. A new activity opens with intent and data goes to another activity, right? So, putExtra().

This is the method that gets two parameters. The first one is keyword. Now, the keyword. I cannot overemphasize how important this keyword is, I would say critical, crucial, you get it, because here we're going to use the same keyword in another activity to get the data, right? So, you can't have different keywords to get the same data. Well, you understand that anyway. Don't get confused. In other words, keywords must have a special name that cannot be used anywhere else. All right. Why didn't you just say that at first? Got me all scared. Okay. Anyway, here we are going to write name for the keyword. That was so difficult. You can, of course, use any keyword you want in your code. It's your code. Do what you will.

So, as the second parameter, write the data which is going to be sent. So, I'm going to write name here. See why I'm consistent? Because the data which we will send belongs to the name string container. Now the penny drops, just playing. Anyway, I want to send email with the same method, right? So, after writing Intent.putExtra, I'll write email as the keyword, and I will write email for the data which we are going to send. All right, so, in that section, sending phone data is the same, so I'll write intent.PutExtra("phone"), phone. And then, well, I got to make the Intent method work. So, here after writing start activity, we should write whatever the name of the object that we created from the Intent classes in the parentheses. That's why I'm typing Intent. All right. So, after all of that, now when we click the 'Sign up' button, the intent method will work and the second activity will open. Now, of course, we don't have the second activity, but we're going to do that later. All right, we're going to continue in the next video. So, I want to see you then.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics