Android Login and Registration with PHP MySQL and Retrofit.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to another android application development tutorial in this video we are going to learn about how to create an android application with user signup and user login for this application we use a remote server with mysql database on it we use retrofit post request here is the application this is the login page of this application first we are going to create a new user so click the new user sign up enter username enter password for the user and the name of the user now click register so here the registration success now i can login but here i try to register the same user again so before going to register the same user again we can check the database this is the table user info now there is no information available we can check the database so here this is the newly created user username pravish at abc.com password password123 now i try to register the same user again prabhish at abc.com and there's some password and the name of the user so this username already available in the database we try to register but this time it shows user already exist in the database now we can test the login section publish at abc.com but here i enter some wrong password and try to login so this time login failed now i am going to enter the correct password password one two three and login so this time login success this is the name of the user now i open the application again enter the name of the user username to be shut abc.com enter password password one two three this time i click the check box remember me and click login so here i successfully login now i remove the application from foreground now open the application again so this time the login page is not appear instead we directly guide to the homepage of the application so now i click log out now i remove the application from foreground now i open the application again so this time again the application shows the login page so in this application we learn about how to perform user login and registration using the profit with mysql database also we learn about how to save the user login status so before going to start the app development we need to set up the server side components we have to create the database we how to create the needed php scripts for perform user registration and user login now go to hosting go to manage website now select database manager so in my hosting i already created a database with the database name test2db here is the database username now go to phpmyadmin so this is our database our database contain one table called the user info so our table contain four columns iot user name password and name so now we need to upload the need that scripts in the server so here i already create the needed scripts for the application so first i will show you a php script called init.php this is the content of this file init.php the purpose of this file is to establish a connection to the database here we have variables like username password database name hostname we establish a connection to the database by using this function mysqli connect for this function we have to pass the hostname username password and database name after establish connection we save the connection to the variable called coa now our second php script is register.php so by using this php script user user can perform new user registration so for this file we need three variables we need three data from the application username password and name of the user we use the http positive method now we need to establish connection to the database so we attach the file init.php so here we test if the connection success otherwise in the connection field we create a variable called status and specify value as fade here we return a json object with one property status and if the connection success first we have to check whether the user already available in the database so create an sql query select star from user info where user name equal to user name and we execute the query using the function mysqli query and if the number of rows of that particular query is greater than zero that means the user already available in the database in this case we specify variable status is equal to okay now create another variable result code equal to zero and we send back a json object with the two properties status and result code so if the user already available in the database the value for result code is zero otherwise that means user not exist in the database we are going to insert the new user to the database so the here the sql query is insert into user info we insert name username and password to our table here also we execute the query and after successful execution of the query we clear the variables that this is equal to okay and here the available result code equal to 1 and we send back the json object with the two properties status and result code so if the user registration success value for result code is 1 otherwise we send back a json object with one property status that is free so this php script perform the new user registration now i will show you the php script for performing login so this is the php script for perform user login here we get username and password from the device here also we establish connection to the database first check if the connection success otherwise we send back a json object with one property status that is failed if the connection success we execute the sql query select name from user info where username is equal to username and password equal to password we execute the sql query and if the number of rows of the query is greater than zero that means user login success in that case we fetch the raw we create available status is equal to okay we set the result code equal to one also we fetch the name of the user we send back this information in the form of a json object with three properties status result code and name of the user so here if the login success value for result code is equal to one otherwise status is equal to okay result code is equal to zero and we send back the json object with the two properties status and result code so this is the php script for perform user login so here i have two html pages for testing login and registration so now we need to upload these files to our web hosting so go to the hosting go to file manager and upload files so this is the file manager page of my web hosting go to public html in public html here i create a folder called login test now open that folder so now there is no files available in this folder now i am going to upload some files i select all the files and upload now all the files uploaded successfully to the the cost so before going to use these php scripts with our application we are going to test it so we are going to test it using these two files test login and testyregister.html so now go to the homepage open website settings select general so this is the entrance of our free website copy this url so now here we create a folder called a login test so first we are going to test the registration so test register.html so create a username specify the username as previouslykbc.com now specify password now specify the name of the user now click register so this is the response from server this is a json object with the two properties status and result code so here the result code is one that means the user registration is success you can check our database so now in our table we insert a new user the password is password here is the name of the user now come back to the page again now i try to register the same user again so in this case this user is already available in the database try to register so this is the response from server this is also a json object with the two properties status and in this case result code is zero that means the user already registered with the database now we can test the login sorry for this just login.html now we can test login this is the username first i enter correct username and password now try to login so this is the response from server so here login success we have a json object status okay a result code equal to one that means login success and here is the name of the user now i try to enter a wrong password and try to log in in this case we have a json object response with the status okay and the result code is zero that means the user login failed okay let's come to the application development so first i'm going to add the internet permission so open manifest folder open android manifesto xml or use permission here we need the internet permission now within the application tag i'm going to add one more attribute called the user's clear text traffic instrument now we need to already need a dependency for this application so open build.gradle file for this application i'm going to use the data binding data binding line project so within the android tag within the android tag add this one build features theta binding equal to now sync project so here processing finish successfully now we need to add the initial dependency for this application so here i add all the needed dependency for this application this is the material library dependency uh here is the profit and here is the json retrofit json converter so here there is a new version is available now there is one point 2.0 is available now sync project again now the processing finished successfully so first we can call the network part of the application for that here i'm going to create some packages create a package called the activities and place all the activities within this package now create another package called models we can put all the model class in this package so i put the main activity within the activities package now in the model class i am going to create a model package i am going to create a new java class called the api response so this class is for handling the response from the server so in this class i'm going to create some variables first one is status this variable is for receiving the status of the request now create another variable called the result code it is also it is an integer variable for the self code now create another variable called a string variable and that is for handle the name of the user if the registration such if the login success we need to retrieve the name of the user for that purpose here i use the available name so now for proper mapping of the profit here i am going to use the serialize the name attribute annotation serialize the name for status it is status now for the result called for the result code the parameter name is a result called yes correct now for the name of the user see life is the name name okay now we need the getter methods for these three variables so i am going to add the getter methods select all these three variables so this is the model class for handling the api response now we can create classes for our profit so for rot profit i am going to create another package create another package profit util i name the packages first i am going to create a class for the client so i name it as api client so in this class i am going to add two variables first one is base url this is the base url of the server and second variable is available for work of it so now for this class i'm going to create a method called the response the return type of this method is a profit get api client so here check the condition for a profit equal to nine in that case we need to initialize the variable profit equal to new orleans builder base url and add the converter factory json converter factory don't create and finally call the build method on it from this method write on variables for a profit so now we can create the interface for a profit so create another classical profit util api interface this is an interface so create the first method but perform user sign in now specify the return type of this method so here the return type is api response so for this method we have to pass three parameters first one is username second one is password and last one is name of the user okay now we need to specify the api endpoint so here we use the poster request so our endpoint is register.php okay also add one more annotation from url encoder now specify the field names so at field for username you have to use the variable name as username now for password password and final one is the name of the user name so this method is for perform the user registration now we need another method for perform user logging perform user login here also specify the return type of this method api response now specify the endpoint api endpoint here the end point is login dot php shall also use the annotation from url encoder now specify the parameters for this method username and password so here also specify the field names username and for password it is password okay so in this application we use these two endpoints register.php and login.php now we can start the user interface of the application so in this application now we have only one activity that is the main activity we are going to use the main activity as the login page now we need another two activities one for the user registration and second for the home page so now here i am going to create two activities select an empty activity this is this is the register activity now create one more activity for the home page home activity so this is the register activity and here is the home activity now i put these activities within the activity package so we have three activities main activity that is the login page home activity and register activity now we can add the needed layout for the application first we can add the layout for the login page so open the resource folder open layout activity main this is the layout for the login page i already create the layout for the login page so this is the layout for the login page here we have a textview for display the app name here we have an edittext for except the username with idtxt username this is the edittext for accept user password with id txt user password this is the checkbox for remember the user login section and this is the login button and here is the new sign up button here we have a progress bar for display the progress reset text review just that simply display please rate okay so this is the layout for the login page now we can add the layout for the registration page new user registration page so this is the layout for the new user registration here is the edit text for intel username with idtxd username this is the edittext for enter password this is the edit text for the enter name of the user here is the registration button user registration button here again we have a progress bar for display the progress of the registration here we have a text view that simply display please rate okay so now we can add the layout for the home page so if the user successfully logging we need to we leave the user to the home page so now this is the layout for the home page so in home page we have name of the application we have a webcam message so here we have a text review that display a message welcome user name so if the user login success we retrieve the name of the user from server and we display the welcome message and here is a button for perform logout so these are the layout needed for this application now we can start coding so before going to start coding i am going to run the project so here we have a we have an error the invoke customs are only supported starting with android o so we have to fix this issue so for fixing this issue open builder dot credit file within the android tag we need to add some lines so at this section these segments of lines compile options source compatibility java version 1.8 target compatibility java version 1.8 and these lines now sync again you know the project sync finishes successfully now run the project okay now the project available on this application so here we have one action we have a toolbar in the layout also we have a default action bar for this application so now we are going to remove the default action bar from the application instead of the default action bar we are going to add the toolbar so open values folder open styles.xml so this is the current style of the application so light dot dark action bar i am going to change that according to light load no action bar okay so now open the activity main.xml so here we already have a toolbar in the layout with id my toolbar so now we are going to add this toolbar as the supported action bar for this application so now go to main activity so in this project we are going to use the data binding library we are not going to use the find viewpi id method so these layouts support data binding so here the root element is a layout attribute here is the ending attribute so if the root element is layout attribute that layout support data binding so now go to main activity create variables for data binding so here the layout name is activity main so here the data binding class is activity main binding i name it as main binding so here there is no link of the set contentview method instead i am going to initialize that variable main binding equal to databindingutil and call this a contentview method first parameter is context and second parameter is the layout file for this activity there is activity main okay now we can set the support action bar for this application so before going to do that i run the project again so here we remove the default action bar from the application here is the toolbar now we are going to set the toolbar as the support action bar so call the method set support action bar main binding code my toolbar so here we can see here we didn't use the find the view by id method instead we can directly use the variable name in the layout now we can set a title for the toolbar so use the method get support action bar dot set title login page so on the project again here is the name of the page as you can see here the data binding library is a very useful resource for an android app developer so first we can perform the new user sign up so within the main activity when you sort of click the button we need to go to the register activity so main binding and our button name is bn signup dot set on click listener from here we need to start the sign up register activity start activity new intent first parameter is the context here it is main activity and second parameter is the target activity class name here it is a register register activity note plus now open the register activity open the layout for the register activity this layout also support data binding here is the toolbar create variable for the data binding class activity activity register binding and name the variable as register binding now there is no need of this kind of view method register binding equal to data binding util dot set content view first parameter is the context second parameter is the layout for the activity activity register now we can set the support action bar register binding it's my toolbar now create now we can say the title for the page so get support action bar so set a title new user signup okay now we can test it i run the project click the button here we come back to the new user signup page now we can perform the new user sign up so now we can start the new user sign up for that create a separate method perform sign up register binding dot b bn register set on click listener perform sign up so here we can display the progress sign signup progress so register binding show progress dot set visibility into visible now i am going to create a separate method for display information to the user display user info in this method need one paragraph same message so here we are going to display the user information using a snack bar so snack bar dot make now specify the layout for using this network so our layout header our root element is a coordinator layout so we can attach the snag bar to the coordinator layout register binding dot my coordinator layout second parameter is the message date parameter is the length of the message shown so snack bar door length short and finally call the show method here after display the user message we are going to reset the password field on the layout so register binding dot txt user password okay also we are going to hide the user progress register binding don't show progress without visibility into con now we are going to use invisible okay so now we can perform the user registration so first we can get the information from user username registerbinding.txt username dot get that string password register binding uh txt password where it is txt user password get things to string and last one is name of the user register binding tote txt name now we can perform user registration so here the response handling is the api response api client object api client that there is some mistake on the method check the api client method yeah this should be a static method now get api client note create api interface dot class and call the method perform sign in user sign in first parameter is the username username second parameter is the password password and last one is the name of the user now we can handle the we can implement the callback response of the request now we can handle the response from server so first we can check if a response don't code equal to 200 that means the request success or not else we can display information to user and display user info something went wrong here we are going to reset the password field of the registration form so registration binding pxt user password dot set the text okay so if the response is okay that means if the response code is 200 we are going to test other condition if response dot body don't get status dot equals okay otherwise we use the same error of info if that is also true we are going to test other condition if response dot body don't get a result code equal to one if the result code is equal to one user registration sections otherwise that means if the response code is 2 the user already accessed so we are going to display user information user already exists and if the user registration success we are going to display some information to the user using a toast legislation success now you can login so the user registration success we are going to we are we need to get back to the login page so for that here i am going to override the onbiteplus method here i am going to call that method on byteplus after that and finish this activity now we can test the new user sign up so i run the project now the project available on this virtual device click the new user sign up so before going to perform new user signup we can check the database now there is one user available on the database i delete that user there is no information available on this table called the user info now we can perform the new user registration abc.com that is the username password is password123 now enter name of the user now perform the new user registration now here shows registration success and now you can login now we can check the database so here the new user address successfully to this database username rubish abc.com passwordpassword123 name of the user is pravish okay now again i go to the new user sign up page i try to register the same user again rubish at abc.com this user already available in the database under some name of the user publish okay so this user already available in the database try to login type try to register so in this case here we got a message user already exist so now we successfully finish the new user signup page now we can start coding for the login page okay now we can perform the user login close all this one our login page is main activity so create a separate method for perform login now create the on click listener for the login button bn login perform login so here we can display the progress login program dot set visibility into visible okay so here also for display user information we can create a separate method display user information with one parameter message display message using snackbar main binding holding into layout message snapbar doped along the short and displayed also we can disable the progress so main painting to show progress dot set visibility into invisible now we can perform login so before going to login we can we need to get the username and password user name main pointing txt username dot get text now get the password main binding tool txt user password get text to string okay now we can perform the api call the response return type is api response api client get api client create api interface dot class and call the method perform login here two parameters needed first one is username and second parameter is the password so now this is the time to implement the callback methods of this api call so call dot and queue okay so now we can handle the response from server first we can check if the is the request is success or not if response dot code equal to 200 otherwise display user information something went wrong okay this is the first condition now we can test the second condition if response dot body dot get status dot equals okay else use the same situation we use the same statement so here we need to do one more thing main binding tool pxt password txt user password don't set to text we can reset the password field the same thing here also okay so now if this one also true we check other condition if if response dot body a result code get a result code equal to one in that means login success otherwise we display the information to user so if the status code is not equal to one that means login failed so if the login success we need to get the name of the user response code body door get name now we can go to the home page home activity so create an intent no intent so main activity dot this and target class name is home activity dot class now we can attach the name to this intent put extra name and pass the variable name now we can start the activity start activity as the intent also we can finish the login page now we need to display a welcome message for the user inside the home activity so go to home activity here also data binding is need to be enabled so private activity home binding and it does home binding combining equal to data binding util dot set content view this now specify layout activity home get the name from the intent get indent dot get string extra name now set the welcome message on the home page so from binding load txt user welcome dot settext welcome name okay so now we can test the user login now we can test the user login so we run the project so we can check the database first so in the database we have only one user with the username provision abc.com password passport123 and here name of the user is pravish or k ok so we are going to test it so first here i am going to enter a wrong use of information a wrong password and try to login so now here the login is failed now i'm going to enter a correct username and password rubbish at apc.com we can check it again publish at abc.com and password is password123 password is password one two three and try to login so now the login is success and here is the welcome message for the user now we are going to learn about how to remember the user login so if the user login for the first time then there is no need to display the login page again if the user logout we can display the login page again so now we are going to learn how to do that so we are going to save the login session using shared preferences api so we are going to save the user login status using shared preferences api so for that we need some string resource so open strings.xml open editor add a new single source first one is preference file key specify value for it pre file key okay now add another string source brief is use okay default value is is user login now add one more string resource name of user name of user okay now i'm going to create another package create a package up util in that package create a new java class called app config so in this class i'm going to create some variables first one is context now create another variables for shared preferences now create a constructor with only one parameter context now i'm going to initialize the shared preferences from the constructor context tooth get shared preferences contact store get string dot string preference file key now also specify the mod of operation is the context dot mod private it is not deleted these get shared preferences yeah okay now we can create another method for check brother user login or not so create a variable called the public boolean is user login so write on sharepreferences.get boolean contact store getstring proof is user login now specify the default value for the operation so here the default value is false now we can we need to create another method for update user login status so public void update user login status so here we need one parameter is a boolean value status so now get some variables for editor shared references editor share preference dot edit now editor dot put boolean contact store get string and dot string dot preference is user login and pass the value status now call the apply method on the editor so here we save the user login status now we are going to create another method for update the save the name of the user if the login success we have to save the name of the user so public vote save name of user with one parameter that is the name of the user should also we need some variables for editor set reference dot edit editor note put string context stored getstring or dot string dot name of user now specify the default value sorry not default value here it is the name of the user okay now save the data now we need to create another method for get the name of save the name of the user from the shared preferences api so public string get name of user so here rethon share preferences dot get stream context stored get string under string dot name of user and here specify the default value unknown okay so we need these four methods first one is is user login we use this method for check whether user login or not uh this is for save the user login status this is for save the name of the user and this last method is for retrieve the saved name of the user so now we are going to remember the user login if user check the checkbox we have to remember the user login so now we are going to create a click listener for this checkbox so go to main activity create an on click method for the checkbox check box click so this is a non-click method so this need one parameter is an object of view class now go to the layout file open design select the check box and search for on click method select the method inside main activity now go to main activity create some variables create a boolean variable is remember user login i put the value false for it now create another variable app.config now first i am going to initialize the variable appconfig new app config pass the context now we have to we need to know whether user selected checkbox or not so go to this method so here we are going to update the variable is remember user login use the parameter view so this is the check box so we type typecast that one into a checkbox on that variable call the method is checked now go to the login section so if the login success we have to check whether user select the checkbox or not is remember login if it is true you have to save the user login so app config dot update user login status into true also we need to save the user name save user name and pass the name of the user okay also when user open the application we have to check whether user already login or not that is the next step so within oncreate method we have to check it here if app config dot is user login if it is true we have to move the user to the home page so here we have to get the name of the user so get it from the shared reference get name of the user then we have to start the home activity so we can use the same court now go to the home activity so in home activity uh we have inside home activity we have a sign out button logout button we can check the layout file activity home so here we have a logout button available so now we need to perform the user logout so go to home activity so from finding tool bn logout though set on click listener here also we need to create variables for upconfig so if the user click the logout button we have to update the user login status app config dot is user update user login status into false also we need to start the login activity so start activity new intent home activity dot this and login our target class is login activity and target class is main activity we can finish this activity okay so now we can test it again so run the project enter a proper username a valid username and password rubbish at abc.com that is the username password is password one two three that is the password so here i check the checkbox remember me and try to login so now here the login success so now i remember i remove the application from foreground now i open the application again so in this case the login page is not up here instead we are directly moving to the home page because we remember the login so now i log out from the application i remove the application from the foreground now open it again so this time the login page again appear so this is how we save the user login section so this is how we create a single login and registration application using a profit post request i hope you understand the concept of this tutorial if you have any doubt or suggestion on this video please mention it in the comment section below if you find this video helpful please share it for your friends and don't forget to subscribe this to this channel thank you for watching see you in the next video
Info
Channel: PRABEESH R K
Views: 13,275
Rating: undefined out of 5
Keywords: Android, tutorial, php, mysql, database, android login example, android register login example, login app, login page, android login app retrofit example, android tutorial for beginners, android mysql database connection, android mysql example, retrofit post request, retrofit post request with parameters
Id: lNaSyOyfdtI
Channel Id: undefined
Length: 72min 37sec (4357 seconds)
Published: Thu Aug 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.