#3 Register Activity | Login & Register Android App using Firebase Authentication with Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right then let's start with the register activity we'll just create a new empty activity and name it as register activity then we will just head to the layout part and we'll start with the layout and let me change this to scroll view just like the way we have done with the main activity and we will give the background as light gray [Music] all right then let's make a relative layout let's add some padding and margin around it and we will make the background color white here very simple then we will create our text view so that we can give some heading to this activity and let's give it an id let's say text view register head [Music] and let's add some margin on the top [Music] all right then you can write any text [Music] and let's align it in the center and we are going to use the text appearance property so that we can use the predefined font style then below it we will define another text view it will be like a label text so it will just say full name and below it we are going to have the edit text for taking the input from the user and the id we can just have register full name [Music] all right then below it will have the edit text to take the input from the user these are pretty simple so i don't think i have to explain this part so i'm just going to do this part quickly you can keep pausing the video and try to follow with the video all right then let's define the background property for this so that we can have a border around the edit text to make it look little different from the text view so we'll just define the background property and here i'll write at drawable forward slash border and as of now we don't have any resource file with the name border but we'll just define it now so we can get into the drawable folder and then create a new drawable resource file then we'll give it name border all right then we don't need this tag instead we are going to use shape tag all right within it let's first define the background color and the color has to be white then below the color let's define the border and we want the width to be 1 db and then the color to be grey and then i want to make the corner of the borders little round so i'm just giving 5 db if you want it to make more round then you can increase the number you can make it 10 db anyways then let's get back to the layout file and i'll just copy the text view and the edit text combination because we are going to use it many times for different fields all right then let's change this to email so it will be text view register email and then it will be below edit text register full name then we'll have to change the other properties as well let's reduce the margin and also we have to change the text since we are taking the email here all right then we'll have to change the properties of the edit text as well then i'll copy the edit text text view and then i'll paste it again and then make the corresponding changes so you just try to follow with the video all right then for the gender we are not going to use edit text but instead we are going to use the radio buttons so i'll just get rid of the edit text and make a radio group and the orientation i want it to be horizontal so that the buttons are like next to each other horizontally let's give it an id radio group register gender since it is a register activity so i'm just using the name register it will be more convenient for us to refer these ids in the java classes later then within it let's define the radio button the first one for the female let's copy paste and the second one is for the mail all right then let me copy the text view edit text combination once again because the next fields are just the direct input from the user we are going to take the password twice from the user first we'll take the password and then the second time we'll let the user confirm the password by entering the password again then finally we will define one register button all right and then below the register button will define the progress bar so that the progress bar is visible on the activity as long as the registration is under process and we want the progress bar to be in the center of this relative layout so we will use the properties layout center horizontal as true as well as layout center vertical as true and we will give it an elevation of 10 db so that it is above all the rest of the elements and in the beginning we want the progress bar to be gone or we don't want the progress bar to be visible in the beginning until and unless the user clicks on the register button so we will take care of that from the java part we will make the progress bar visible from the java so i think this much is fine for the layout part we can just get into the register activity class all right and let's start by defining the edit text variables so that we can take the input from the user we have to define edit text variables for all the edit text that we have used in the layout that is full name email [Music] date of birth mobile password and confirm password and then we will define the progress bar as well same goes for the radio group that we are using for the gender and then we will have to define the radio button we just have to define one radio button which is going to correspond to the button which was selected it can be either male or female alright then get inside the oncreate method and we'll just set the title of the activity first and let's display one toast in the beginning of the register activity so this is how we define one toast message that is toast dot make text then the make text procedure or the function has three parameters first is the source activity or the context from which we are going to display the toast the context here is the register activity so we will write register activity dot this then comma then second part is the string that we want to be displayed then comma then the third part defines the duration of the toast so here i'm just defining long so it will stay on the string for a longer duration we have length underscore short also then finally we'll just use dot show so that the toast is visible all right then let's find the views of all the variables that we have declared let's start with the edit text variables then let's find the view for the radio button then finally we can define the register button and since register button is not going to be called or used from other methods so i'm not defining it globally i'm defining it locally in the oncreate method the other variables or the other edit text variables are going to be accessed by the other methods apart from the oncreate method so that's why i have defined them globally anyways then let's set one click listener on the register button so first of all we'll get the id of the radio button which was selected so for that we'll just define a integer variable selected gender id and in this variable we are going to save the id of the radio button which was selected or which was checked so for that we need to use the get checked radio button id with the radio group variable all right then we will get the view of the radio button which was selected since we already have the id of the button which was selected let's say there were two buttons one for male and one for female let's say female button was selected so we have now saved the id of the female button in the variable selected gender id so using this id we can find the view of the button which was selected after that let's just obtain or extract the data which was entered by the user into different edit text so for each of them i'm going to define one string variable where i'm going to save the data and to obtain or extract the data we need to use the edittext variable then dot get text this will get the text then dot tool string to convert it into string all right then for the gender since we cannot obtain the value before verifying if any button was selected or not let's say the user did not select any of the button or did not check any of the button in that case we cannot get the value so we will just define the variable here then let's start with the if else statements text utils dot is empty to define it simply we can just say text utils is a set of utility functions which are used to do operations on string objects so here we just want to check if the variable is empty or not so for that we are using is empty this method will check if there is any text in the variable or not so if full name is empty in that case we have to let the user know that they are supposed to enter the name and for that we'll just quickly define one toast message then after that we have to display the error on the edit text which corresponds to the full name so we will set an error to the edit text and to display the error in the edit text we have to request the focus as well on that edit text then we can define the else if part and we have to check if all the variables have been entered value or not if any of the variable is empty then we have to let the user know that they are supposed to fill up the values without which we cannot let the user register on the app so we will do that one by one now and to verify if the email address that was entered by the user is a valid email or not we are going to use the patterns to use regular expression in java we have to define a pattern for searching or manipulating the strings and for some of the most widely used or more common strings the java has already predefined regular expressions or the rejects like email so here we are going to match the pattern of the email address with the email address that the user has entered and then we will match whether these two patterns match or not if they do not match then we have to display the error for the date of birth we are letting the user enter the data directly right now but we are going to define the date picker after we finish with this activity then let's check if any of the button of the radio group was checked or not so if the id of the check button equals negative 1 that means none of the button was checked or selected so in that case we have to display the error again and the number of digits in the mobile number cannot be more than 10 or less than 10 it has to be 10 digits we will define one pattern for the mobile number also but let's just continue with the flow let's just finish this then we will define the pattern as well and if the length of the password is less than six digit we do not want to accept it as it is going to be a very weak password then finally we will match if the password and the confirm password matches or not if they do not match then we have to let the user know to re-enter the passwords so the control is going to pass to this else part if all of the conditions that we have mentioned above with the else if are false that means the user has entered all the values and all those values have been matched or validated that means now we can finally move to register the user on the app all the data have been verified and the data is correct or validated so the first thing we'll do is to obtain the text of the radio button which was selected since now we can assume that one of the radio buttons was selected that's why the control is coming to the else part so we can just directly get the text from the radio button then we will set the progress bar to be visible because now we are going to start with the registration process let's just call one register user method and within this method we will pass all the string variables that we have and then we will define this register user method now let's start with the firebase so to use firebase in our project first of all we have to add the required sdk that is the firebase sdk so we'll just click here on tools and then scroll to firebase then among these options we'll just use authentication click on it and then you can click on the last one authenticate using a custom authentication or even if the options that are displayed to you are different from this you don't have to worry because the first few steps are same for all of them that is we have to connect firebase to our app and then we have to add the firebase authentication sdk to our app then you click on connect to firebase it will open a new page in your browser and it's going to load the firebase console you have to accept the terms and condition then you click on create a project then you can give any name to your project all right continue then i accept the google analytics terms and continue then the firebase is going to create your project and click on continue then we can just go ahead and connect our app with the firebase and it is showing connected then click on add the firebase authentication sdk to your app this is going to add the necessary dependencies to your app so everything is good to go then we can just start using the firebase authentication so just write firebase auth that is for the authentication and let's give the name to the variable as auth and still it is showing as error because we have not imported the required firebase library so either you can just directly import the firebase auth or you can just keep on typing and the android studio is going to do that for you so we have obtained the instance of firebase authentication and we have saved that instance into the auth variable now using that variable we are going to create one user using email and password so for that we have to use create user with email and password method and within this method we just have to pass the email id as well as the password then we need to make sure that the user creation was successful so for that we will use add on complete listener and as the name suggests this function or the method is going to fire or is going to execute when the user creation is successful then the context here is the register activity and then comma new on complete listener and you can just hit enter on the autocomplete and this on complete method will be created now here if the user creation has completed successfully then this on complete method will be executed now here you can do whatever you want or you can intimate the user that the profile was created or you can open the user's logged in profile from here and you can also take care of the exceptions or the errors that might have occurred so i'll just use if statement and i'll check if the task was successful and for that we will use the method is successful so that means if the task was successful that means the user has been created and since the user has been created let's just send one email verification link to the user so that the user can verify that he or she is in fact the owner of the email id that they have entered during the registration process so in that case i'll use firebase user and i will define a variable for the user then i'll get the current user using the auth variable that is the firebase authentication variable then let's just open the user's profile after successful registration so for that we'll just have to use intent and here we will set some of the flags with the intent now the intent flag that is the flag activity clear top if we use this flag then let's say the register activity since we are starting the activity user profile let's say the user profile activity is already running in the current task then instead of launching the new instance of the user profile activity we can just destroy all the other activities on top of it and let this intent be delivered to the resumed instance of the user profile activity which is now on top because in most of the case if you are using intent to start a new activity a new instance of that activity is called and created which means a new instance of that activity is popped and placed on the top which is in most of the case undesirable like most of the time we would want to reuse the activity and refresh its views rather than destroying it and then recreating it then the next flag that we are using is the flag activity clear task this flag can only be used in conjunction with flag activity new task so let's talk about the flag activity new task first so if an activity do not exist in an already created task then it starts the activity in a new task with activities new instance at the root of the task back stack else the task is brought forward with the activities last state restored and this activity receives the new intent in on new intent method so which means only one instant of the activity can exist at a time in this case the back button is still able to return to the user to the previous tasks activity then using the flag activity clear task will make sure that the back button cannot be used to return back to the previous activity or this clears any existing task that would be associated with the activity before the activities is started and this activity then becomes the new route of the task and all activities are finished so anyways we don't have to get into too much detail of these flags the main purpose that i'm using these three flags is to make sure that the user cannot return back to the register activity after successful registration because it doesn't make sense if the user has completed its registration then the user is supposed to move to the user profile activity then we will just start the activity with the intent and let me just comment out this part so that we can run and test our app and also we have to put a semicolon at the end of the add on complete listener method you can see what i'm talking about here and before running the application we have to comment out the login button in the main activity class because we have not defined the login activity as well so we can just comment out this part for now and then let's run the app all right so the login button is not working because we have commented out the login button and the register button is also not working so let's just quickly check what is the problem here it is saying that we are attempting to invoke a virtual method that is the on click listener set on click listener on a null object so let me just check so we are finding the view using button register let's just check if the same id we have used with the register button or not in case there is any spelling mistake it won't work all right so we have not defined the id in the layout for the register button so we can just define the id as button underscore register all right now the app is running so let me click on register right so now the register activity is working as it should so let's just quickly enter the data and try to see if we are able to register the user so you can see the email has to be a valid email also the mobile number has to be 10 digits then the password should be at least six digits and if the passwords do not match then also we cannot register and let's click on register and still we are getting some error now the error is on the progress bar so let me see where i have defined the view for the progress bar all right so that is the problem that we have not defined the view for the progress bar and we are still using it so we cannot do that we have to find the view of the progress bar before we can set the visibility of the progress bar let's run the app once again and let me just enter the data quickly and let's click on register and you can see after clicking on the register the progress bar is visible but the progress bar is not stopping because we have not written any code regarding that it will just keep going so anyways let's just get into the firebase console and check if the data of the user is there or not or if the user has been created in the firebase or not [Music] so in the left side just click on the tab build from there you can click on authentication so a user cannot be created until and unless we set the configuration from the firebase which are the providers that we are going to use and since as of now we have not used any providers and that is the reason the registration got stuck it did not vent all the way through so quickly come to the authentication part and under the sign in method we have to use the email password and we have to enable it then you save it [Music] now run the app once again [Music] now this time i hope the registration is going to go through and we will be able to finish the registration of the user [Music] and you can see we have the toast that user registration successful which signifies that the user has been created in the firebase so we can just quickly go ahead and check whether the user is there or not and you can see the user has been created with a unique uid which is a user id you can see the date of creation you can see the last sign in all right so this was it for this video in the next video we are going to do the exception handling in the registration part that is the exception handling in register activity because there might be many cases where the registration cannot be completed like let's say one user has registered successfully with one email id then let's say another user is trying to use the same email id to register on the app that is going to throw one exception so in the same way there might be many more errors or exceptions that we need to take care of so hopefully we'll see that in the next video thank you
Info
Channel: tanzTalks.tech
Views: 1,623
Rating: undefined out of 5
Keywords: tanzTalks, tanzTalks.tech, login and signup in android, login in android studio using firebase, login and signup in android studio with database, android login and registration with firebase, android registration, login and signup in android studio, login in android, android login, signin and signup android studio, login and register android studio, Login and register in android studio, Android app, firebase authentication, firebase app, login and register android app
Id: qyH6HavVn_k
Channel Id: undefined
Length: 49min 29sec (2969 seconds)
Published: Sun Aug 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.