Login and Register Form using SQLite Database in Android Studio | Login Registration Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to my youtube channel in this video i am going to show you how to design registration and login page using sql lite database so without wasting any time let's start with that so first thing what we have to do is we have to create a new project for that click on create new project select empty activity click on next and here i'm going to name this one login sqlite like this after this i have selected this minimum sdk version that is 4.4 and click on finish this will create a new project within few seconds so here our new project is created so first thing select this activity underscore main.xml and first we have to design the registration page and to the design registration page we have to actually import some images inside this one so i have these basic images you can use any images just copy these move back over here right click on this and paste it click on ok refactor and here you can see this bg images and bg images one login and registration is added so first thing click on the split so that xml file code is visible over here and first thing i have to remove this textview i don't need this after this we have to change this constraint layout to relative layout because we are going to work on relative layout so that's why we have to change that after this we have to search the background set the background for this so background and background will be at the rate we have stored the image into the drawable so i'm going to use this one and i'm going to use this pg1 this image and this one is looking nice after this what i have to do is i have to set padding for this one so i'm going to set the padding 10 db and after this now inside this relative layout we have to first put some image so i'm going to put an image imageview and i'm just set the id width will be match parent and height will be 200 dp and after this i'm going to set the id for this one so id will be image view after this i have to set the source for this image so it will be drawable i'm going to set the registration this one and after this i have to set the scale type and i'm going to set this one center crop little bit enlarged this one is looking nice right after this we have to set the text view not text view it is edit text there is a text box so we have to set the edit text so here we have to set the width that is match parent and height is wrap content and we have to set the id so id will be user name after this we have to set the hint for this one that is username and text size will be 25 db after this we have to move this edit text below this image so we have to set layout below let me just write below layout below and set the id that is image view we have to set this below the image view and here you can see this now this image view now this edit text is on the proper position after this we need one more edit text for that what i have to do is i have to just copy this one and paste it once again and change this id first to password and change this one below to user name like this and we have to set some margin and change the hint to password and layout top margin so margin top will be 50 dp and we have to set the size oh everything is ready next thing we need one more edit text to re-enter the password so i'm going to copy this and paste it again and change the id to re password and this one below password change the hint to re enter password like this after this we need few buttons that is one is for registration and one is for one is for registered user that is sign up and another is for sign in so i'm going to add one button over here button and the height width will be match parent and height will be wrap content and i'm going to set the id for this one so id will be bt and sign up after this i have to set the text so text will be register after this i'm going to just disable this text all caps to false after this i have to set the this button needs to be over here below this re-enter password so below id will be password below this one and we have to set the background trend that is the background color and i'm going to set this one to at the right android and i'm going to set color holo blue dark this one is looking nice and we have to set the text size so text size is 25 pp after this we have to set some margin to the top so top margin margin top will be 20 dp and now we can see this this one is looking nice here you can see this after this we need to add one more button for that i'm going to copy this and paste it again i'm going to change this one to sign in and what we have to do is we have to just change this one to below btn sign up and we have to change the color also so change this one to dark to light and we have to change the text as well so change this one to existing user and i want to change the line after this i have to write go to sign in page and here you can see this now this one is looking nice okay so now the design is ready here you can see this next step what we have to do is we have to create a new class that will be the tb class so let me minimize this layout for these right click on this click on next java class and we have to name this one db helper and click on enter and here we created a new class and here first thing what i have to do is i have to extend the sqlite open helper this one after this you have to simply click on alt and insert and it will ask to create a constructor so click on this and select the first one click on ok this will create a constructor and here you have to just remove these all stuff we don't need these and also remove this nullable and we have to also remove these all and in context and after this we have to write the database name that will be login.db and after this it will ask for the factory so that is null and the version will be one and here you can see this it goes after this what we have to do is we have to again press alt and insert okay so it's not showing here you have to just go over here and click on implement methods and you have to select these both and click on ok it will implement two methods that is oncreate and on upgrade so here what we have to do is on create non-create we actually create the data create the table so what we have to do is we have to just write like this here we have created the db that is login db and inside this db we have to create the table so here i'm going to create a table so my db name is over here that is db so i'm going to change the name to my i'm going to change this one to mydb like this mydb so that i can easily identify so mydb dot access sql and inside this one we have to write create table and table name will be users and there will be two columns that is user name and this will be this except the text type and the first username is nothing but primary key after this we have password this this will also be text like this now this will create a table with the name users and there will be two columns that is username which accept text and another column is password that is also of text type okay so next thing inside the on upgrade it means that whenever you are upgrading your application then what you want to do with the database or with the table so i want to drop that table so i'm going to write that query for that so drop tables table if exist and you have to mention the table name because whenever you upgrade it will drop that table if it contains users and new database will be created with the help of this constructor okay after this what we have to do is we have to write few methods to actually insert the data into this one and to fetch the data of the users according to the username and password and also you have to check that this user exists or not suppose someone is trying to register with the name kumar which you already have in the database then we have to check that that user already exist for that if you have to write three methods so first method i'm going to write that will be the public and it will return boolean type value so boolean and the method name will be insert i'm going to name this one insert data and this will accept two parameters that is string user name and second will be string password after this it will shows an error because we haven't returned anything yet because it's expecting the boolean type value either true or false that's why it's showing an error so in this we have to first create a we need actually sqlite database open instance so sql lite database i'm going to name this one mydb is equal to this dot i need get right table database after this we need one contact content values this will be content values to show the values and then finally pass that in the query so new content value we have to create the object for this one and after this we have to put the values inside this one so content dot put and with that column name so user name and the value will be user name and finally the another value is dot put and this one will be password make sure you write the same column name and this one will be password like this after this we have to store the value so long result is equal to my db dot insert and inside this one we have to mention the table name so our table name is users and we have to pass the null value for the second parameter inside this one that is null column hack and finally we have to pass the value if the this one the data insertion is successful then it will return some other values if this one is failed then it will return minus 1 and that minus 1 will be stored inside this one so minus 1 represent the task is failed so if result is equal is equal to minus 1 it means that registration is failed so i want to create a toast for that to show the user create toast and not toast i don't want to create a toast okay so we have to return false if this one is yeah this one is failed then it will return false in the else condition it will return true if it's if this one is successful in the else condition return true like this now our insert data method is ready this will insert the data and it will return either the registration is successful if the successful then it will return true and if it's not successful then it will return false we need one another method to check that if user exists in our database or not so i'm going to name this one public and it will also return boolean type values so check chooser and like this and this one is also going to accept one variable that is user name like this and here what we have to do is we have to again get the right get right table database so for that sqlite database my db is equal to this dot get right table database inside this one we need this time cursor like this and here we have to write my db dot raw data draw query actually and say this one we have to pass the query so select the star from users where user [Music] name is equal to question mark and we have to pass that value so new string and finally we have to pass this user user name like this this username whatever when this method is called the username is passed with inside this one this parameter and this username is finally assigned to this question mark so this will select the data if this suppose i'm trying to check for sort of then it will check for sort of if the data exists then this cursor will have some data so we are going to check if this cursor has some data or not so if this have data it means that registration that user id or username is already in our database so if cursor dot get count is greater than zero it means that it has some data so we have to return true else we have to return false okay so whenever there is return is returned return result is false then we can register with that username right so our second method is ready so we have to write one more method that is nothing but to check the username and password that will be the use while we are logging in so i'm going to write this one so public and this will also return boolean check user name password like this and this will accept two stuff that is user name and second is password and again we have to we actually need these two so i'm going to copy this and paste it over here and here we have to modify this one a little bit we need user name and password is equal to question mark and here we have to write like this username comma password and our query is ready and again if this one is available if cursor dot get count is greater than 0 then return true else it will return false now the task for this db helper is done in this one we are actually checking for the username and password this method will be called whenever the login button is clicked next thing what we have to do is we have to move to this main activity now the task for this tv helper is done we are going to refer this tv helper while we are coding so move back over here at the main activity here what we have to do is in the this one in the xml file we have created three use edit text box so we we have to actually extract the text from these first register then extract so first i'm going to register all controls three text boxes edit text and two buttons so move back over here and here we have to write these so edit text and we have to write user name password and read password like this and we have some buttons so button btn sign up and another button is btn sign in and we also have to create one db helper object so db helper my db like this and finally move inside this oncreate method and here we have to actually register those text box edit text and button so user name is equal to edit text find view by id r dot id dot user name like this after this we have password password is equal to edit text r dot id dot password after this we have free password edit text find view id r dot id dot read password and finally we have buttons so we have two buttons so ptn sign up is equal to and this one is button so button find view by id r.i.t dot pdn sign up we have bdn sign in find view by id r dot id dot ptn sign in now we have registered all the stuff and we have to just create object for that mydb that is dbhelper mydb is equal to new db helper and we have to pass the contact so this is the context finally what we have to do is we have to create a click event for these sign up and sign in button so first i'm going to create a click event for the sign in button so for that what we have to do is we have to just move back over here and here we have to write ptn sign up dot set on click listener this first one just click on this and inside this one we have to write new view and on click listener you have to select the second one and hit enter and here you can see this automatically method is implemented that is overwrite it and inside this one first we have to extract the text from these text boxes edit text so first i'm going to extract this string user is equal to username dot get text dot to string after this we have to extract password so pass is equal to password dot get text dot to string then re password so string repass is equal to re password dot get text dot 2 string now we have all the values required to actually register so first thing what we have to check is either none of these textbox or edittext needs to be empty for that if users dot equals null it means that we don't want we want to show the user that you have to fill all the friends in the same way we will check for the pass dot equals null this is or and finally we have re pass dot equals null if any of these are null then we want to show user a toast and what toast i want to show i want to show the toast that fill all the fields like this now in the else condition let's check till now now i'm going to start this and let's check if this shows or not so it's starting my emulator and here my informator is started now real build is running install and launching activity and here you can see this now the design is login sql lite and click on this one and here you can see this fill all fields the message is showing okay so now i have minimized this in the else condition what i have to do is first i will check that if pass dot equals what we have to check for equals that is re-pass if this is true then we want to perform further operation if this one is false then i want to show a message that is that password doesn't match so i want to show the toast password not match like this inside the if condition after this after checking this that uh password and re-pass re-enter password is equal then we want to check another thing that is username user exist or not for that we have to work like this so my tb dot check user username and inside this one we have to pass and as i have told you that this method inside this one let me just check this check username and this method is going to return some boolean type value for that we have to store this value and type value inside a boolean type variable so boolean and this one will be valid user result i'm going to change this one to result user check result is equal to and this will store either true or false so if it returns false if user check result is equal is equal to false it means that we are good to go it means that no record formed it for that particular user in the else condition we have to show the toast user already exist so user already exist and i want to tell the user please sign in okay so after this now user is also checked this one is done false it means that here we can see this select to start from star from users where username is whatever username is passed inside this one and that will be checked inside the database if that username is found in the database then that will return some value and that will be stored inside this cursor so now the count for inside this cursor will be increased to either 0 or 1 so if this one is greater than 1 it means that it has some values greater than 0 it means it have some values then it will return true in that case we are not good to go in that case we need to show this message that is user already exists please sign in and in the else condition it will return false it means that user does not exist so that way i have checked this for the false condition after this we have to check for that that insert operation so we have to finally insert the value so i have to insert data so that will be my tb dot insert data and here we have to pass the user name and password okay so this will register our user but if we execute this then this will register our user but we won't get any response that user will be unaware that either registration is successful or not in that case how to check that if registration is successful or not so here we have this method insert data and in this data this method is going to actually return either true or false this one will return false if the registration is failed and this will return true when registration is successful so again we have to store the value into a boolean variable so boolean reg result is equal to inside this one and finally i have to check if reg result is equal is equal to true then what we have to do is we have to show the message toast and here we have to show the message registration successful like this and in the else condition i want to show registration dot register or registration field okay so let's start this again and let's check if this works perfectly fine or not so i'm going to register for kumar okay so let me close this and start this again here we have this and here you can see this when i click on this it will show nothing this one is true now register and registration is successful and here when i click on this fill all fields first credential is working that is we are checking for all is empty or not and second is password not matching so i have written kumar over here and here is kum so register password not matching this time i'm going to match this with kumar and kumar register user already exist please sign in because previously i tried but that time that toast message is not showing that's why it's showing that okay so now back this and we are going to register with some other username so let me start with this so i'm going to name this one srv and password will be srv and srp now register and eriksen's registration is successful if you try to register again user already exist and please sign in this one is looking very still on what i want is whenever we click on this and if registration is successful then we move to the login page there it will ask from the user to enter username and password so for that what we have to do is we have to create one login page first so right click on this click on new and select activity and here i'm going to select empty activity i'm going to name this one login activity and click on finish it will create a new activity and here what i want is whenever registration is successful i want to move to the login page so here my result registration result and this one is successful then i want to move to that registration that is login login activity so intent intent is equal to new intent inside this one i'm going to pass this get get application contacts that is a the current context and where we have to move we have to move to the login activity dot class and finally start activity and inside this one we have to pass the intent now start this again and let's check if this works or not terminate this and this time i'm going to just write for new username that is abc and password will be abc and third pass free password will be abc register registration user is already registered okay so we have abc so abc i'm going to write one two three register and here you can see this registration successful and we are on the login page so inside login page we haven't designed anything so that's why it's showing empty so now registration the task for registration is done so let me close this and next thing we have to code for this another button that is this one existing user when we click on this we need to move to this login activity for that move back over here and inside this one we have to write just pdn sign in dot set on click listener new view on click listener and here we have to simply write the stuff that is intent intent is equal to new intent and inside this one we have to pass get application context and we have to pass this login activity dot class and finally start activity and inside this one we have to pass the intent now start this and let's check if that works or not so in this one second button we have coded for this one whenever we click on this we move to the login activity so this is working fine after this now the coding for this registration part is done now we have to design this login page so let me minimize this move back to this activity login xml and here go to the coding part that is xml click on split and here first thing what we have to do is we have to just remove this constraint layout i'm going to change this one to related layout and here i want to set the background first so background will be same so at the right drawable pg image one this one and i'm going to set some pairing so padding will be 10 dp and after this i need two text boxes and one image box so first i'm going to add one image box or image view and the height will be match virtually match parent and height will be 200 dp and i'm going to set some id for this one so id id will be image view and after this i have to set the image for this one so src it will be drawable at the right tribal login this one and finally we have to set the scale type center crop and here you can see this one is large now this one is looking better after this we need to edit text box so edit text and inside this one we have to set the height and width so match parent and wrap content that will be the height let me just set the id for this one so id will be username and this time this is login username login i'm going to set this one username login and after this i'm going to set the hint for this one so hint will be user name after the hint we have to set the margin top that will be the 50 dp and we have to set this below that image view so below it will be below image view and we have to set the text size so text size will be 25 sp now this is looking better after this we have to set the same thing but for the password so i'm going to copy this and paste it again and change this one to password password login and we have to set this below username login and we have to change the hint change this one to password and now this one is looking nice after this we need one button that is signed in button so i'm going to add that quickly so button the height will be match sorry breath will be match parent and this will be wrap content and we have to set the id so id will be bt and sign in vtn i'm going to change this one to log in not sign in just log in after this we have to set the text i'm going to set this one login and i'm going to set the text size to 25 sp and here you can see this i have written this one in the small layers but it's showing in the capital so what we have to do is we have to just change one property text all cap change this one to false and here you can see this now we can see this one small after this we have to set this layout below that password so the password login password like this and we have to set some background color so for that i'm going to set that background tent and i'm going to set android and i have to set color orange not dark yeah dark will be good here you can see this this one is looking nice and we have to set some margin top margin so i'm going to set some top margin to 25 db and here you can see this now but design is ready and this one is looking really nice now start this once again and let's check if this one is look looking nice or not okay so terminate this and click on this and here you can see this we have now the login page okay so now what we have to do is next thing we have to move back to this one that is login activity double click on this inside this one we have to register first all these buttons and edit text box edit text so over here first thing what we have to do is we have to just write like edit text user name and the second will be password and we have one button so button and this is btn login and finally we need one db helper mydb like this and we need one home page where we have to move when we click on the login so once this part is done then i'm going to add one another activity for that okay so first i'm going to register so the username is equal to and this is edit text so edit text this will be find view by id r dot id dot user name login password this is also edit text so edit text find view by id r dot id dot password login like this and after this we have to register that button login so ptn login is equal to button type so button find view by id r.id dot ptn log in like this finally we have to create mydb helper tvhelper class object so mydb is equal to new db helper and we have to pass the context so this is the context after this we have to set the click listener for this pdn login so vdn login and inside this one dot set on click listener new view on view on click listener and now override the method on click this will generate a click event and here finally we have to write the click method inside this one we have to verify the username and password from the database for that we have already written one method inside this one that is check username and password inside this tp helper so i'm going to use this method to check either the user exist in the database or not so move back over here that is login activity and here first thing i need when username and password so string username is equal to username okay so let me change this one to user and here username dot get text and convert this one to string and next thing we need password so string pass is equal to password dot get text dot to string and we have now username and password so finally what we have to do is we have to first check that either these two edit text is empty or not so for that if user dot equals null or users dot equals null not user this time password so it will be pass and if this is null then we want to show the toast message to the user that fill all fields or please enter [Music] please enter the credentials in the else condition both text boxes are filled already text is filled then we have to check the user from the database so we have to call the method that is mydb dot check username and password inside this one we have to pass the user and password and let me move back to this db helper and inside this one this method is also going to return a boolean type value either true or false if that username and password match in the database then it will return true and else it will return false so if move back to this yeah here so we have to store the value first so it will be boolean and the result i'm going to name this one variable result and if the result is true it means that the user is valid so if result is equal is equal to true true it means that this user is valid then we have to move to the dashboard or the home page and else we have to show a toast message so toast i'm going to show a message that invalid credentials and let me first write a method or sorry let me create a new activity that it will be the home activity so click on this activity and select empty activity click on this and i'm going to name this one to home activity click on finish this will create a new activity and let me close this main activity for now move back over home activity click on a split and here we have to actually set this one background will be at the right drawable image this one this one is looking nice now for this i have to set one textview so this will be match parent and match parent just let it be and i'm going to set the text welcome home so that we can see this whenever we log in text size will be 35 sp and i'm going to just set this one in the center so center center center in parent okay let me just change this one to center and parent make this one true and we have to set the text color so text color will be color white and here you can see that this one is looking nice so whenever this let me move back to this login activity here whenever the login is successful or the username and password matches then we have to move to this dashboard or home page so intent intent is equal to new intent inside this one we have to write get context get application context and we have to write the activity name that is nothing but home activity dot class and finally we have to start then activity that is intent like this and if the login is successful so now let's start this and let's check if this works or not terminate and here we go so we have registered with abc and abc right so if i log in and here you can see this we are on the login page move back over here click on a again the wrong password and click on this invalid credentials so this one is working fine if i change the username and login invalid credential this one is working so now i'm going to register with username that is something x y z and i'm going to just write x y xyz and this one also xyz and if i click on register registration successful and we move to the login page here we have to write xyz and xyz and when i click on login we successful login so everything is working fine so that's it for this video guys i hope you enjoyed coding and designing with me so please subscribe and share with your friends and also follow us on instagram and we just subscribe to keep updated with our new content thanks for watching
Info
Channel: BTech Days
Views: 19,230
Rating: undefined out of 5
Keywords: simple login app android studio, login and register android studio, android studio tutorial for login and register, login screen in android studio, login and register android studio firebase, login and register page in android using database, registraion and login form in android, login and registraion form in android using sqlite, how to login and register in android app, beautiful register and login in android 3.4, login and registration in android
Id: o9CVZ1gQgQo
Channel Id: undefined
Length: 48min 26sec (2906 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.