‫Welcome back.
‫In this video, I'll mention a very important concept, which is how to get the data from edit text
‫and pass them to the main activity using data binding and using the intents, of course.
‫So I want from you to focus with me.
‫For that I'll create a new variable called Click Handler.
‫And the type of this variable is a new class that I'll created.
‫Now add new contact Click Handler.
‫This class will handle the click events on this button and direct the user from this activity to the
‫main activity.
‫Getting the name and the email from edit text.
‫For that, I'll create a contact object in order to pass the name and the email to it and assign them.
‫And I need a context object because I am going to use the intents.
‫I'll create the constructor having these two variables and I'll create a new method called on Submit
‫button.
‫Click.
‫Don't miss to pass a view as an object.
‫This is for data binding.
‫Always make sure when you are going to execute any method, you should add the view as a parameter.
‫Then let's check if contact dot get name doesn't equal to or equal equal to null or contact dot get
‫email equal equal to null.
‫I don't need to make anything.
‫I need to display an error message telling him that fields can't be empty.
‫Else I need to pass the data from the edit text to the main activity.
‫So else here I need to start intent.
‫I equal to new intent and I'll use the context object that I created to get the current destination
‫context.
‫And next destination is the main activity dot class.
‫And I mentioned this in the intent section.
‫I need to put extra to put more information to this intent.
‫I equal to put extra and here I'll pass key value pairs one for the name and one for the email.
‫And the name and the email.
‫Two variables are of type string.
‫The key of the name would be name and the value of it.
‫It's get it from the context dot get name and I dot put extra the key of the email is email and make
‫sure that you'll type them later on in the main activity.
‫In the same way you type the key because you need to specify the key and it's a case sensitive to get
‫the value context dot get email.
‫Then I need to start the activity context dot start activity and I need to pass I.
‫Now the last step is to link the data binding with function that we created here on submit button.
‫Click and link the edit text with the contact object.
‫Here I'll create.
‫Com dot master coding context manager app and here I need to specify add new contact click handler.
‫Then let's move down to the edit text here.
‫It's very important step to specify text equals and when using two way binding, you should mention
‫this equal sign contact dot name and the second edit text is text at equals contact dot email.
‫Then let's move to the button.
‫And here we should mention onClick attribute at without the equal sign handler to Collins on submit
‫button click.
‫The last step is to move to the Add new Contact Activity class and initialize the data binding private
‫activity.
‫Add the new contact binding binding, and I need to create an instance from the class that we created
‫for the click handling.
‫Object type cont.
‫Then I'll move to the Oncreate method.
‫Contacts equal to new contacts equals data binding util dot setcontentview.
‫And here I need to specify the layout or dot layout dot add new count.
‫This refers to the context of this add new contact activity.
‫Then I need to set the contact.
‫I need to pass this contact object and to pass it to the binding binding dot set contact to contacts.
‫This is a very important step.
‫Many people miss this.
‫I need to create and initialize the handler new add, new contact handler and I need to pass two parameters.
‫The first one is the context and the context of the Add new contact activity.
‫And the second parameter is the contacts object that we created before.
‫I need to switch between them because the first one is the context and the second is the context.
‫Then I need to link this handler with the data binding.
‫So set click handler to handle.
‫What we've done here is to create and initialize the binding the handler and to create a new object
‫called contacts and assign it to this variable.
‫And the click handler is passed here and assigned to this by using set click handler.
‫Let's run, click on this fab.
‫If I don't add anything, fields can't be empty.
‫Those messages will appear.
‫I'll add John john@gmail.com click add contacts to room and here we go.
‫We are directed to the main activity and having the intent extra, which is the name and the email.
‫In the next videos we're going to insert email and name that we passed here into our database.