Login and Signup using SQLite in Android Studio | Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will be creating login and sign up using sqlite it is kind of tricky because we will be dealing with queries and local database but don't worry I'll make the concept as simple as I can also sqlite is not limited to login or sign up okay you can literally store anything in it cut it I have previously created a video on login and sign up using Firebase authentication so if you are interested in that you can click on the I button to watch now coming back to the video Let's create the project choose empty views activity then click on next name it as login sign up SQL and then click ok let's do the prerequisite first so go to colors.xml press Ctrl D to duplicate the line write the color name and screen and hex code AS 4fa 853 and done now go to themes.xml uncomment this slide and right color name as green press Ctrl D to duplicate the line and write item name as color primary variant then again duplicate the line and write item name as Android status bar color and then go to build.gradle here we will add view binding so write build features and inside that write view binding as true then click on sync now and done now to make a edit text look more fancy we will create a border around it so right click on drawable folder then new and then drawable resource file write the name as edit text underscore border then select shape and click ok give it a shape as rectangle as I said it's a border so we will use stroke attribute give color as green and with s2dp now I want the border to be a little rounded so for that I'll use Cornell's attribute and write the radius as 20 DB and done then as I always say I don't want to waste your time in designing so for that I will use a background so let me copy paste it and then again I'll say please don't directly use image like this way instead create this background using jetpack compose or XML don't go for shortcuts okay next thing in prerequisite is to create obviously a login and sign up activity so let me quickly do that make sure you tick the launcher in login activity and then see we have a login activity and sign up activity both now last thing go to Android manifest here we want our first screen to be login activity screen so for that we tick the launcher right but by default main activity is a launcher so we need to remove this intent filter and make exported as false and that's it why I do this because if you won't remove extra intent filter then it will create two apps with two different launcher screen got it so make sure you avoid that situation and with that said we are done with all the prerequisite close all the tabs let's move to our designing part so go to activity signup.xml let me give you a quick overview the designing is very simple we will have to edit text one is for username and another one is for password then we will have one button for sign up and lastly we will have a text view that will redirect the user from sign up screen to login screen and that's it let me quickly write the entire code and then I'll explain you the important parts and then see we are using constraint layout I have added the sign up background then I have created an edit dance with the IDL sign up username and other attributes then again we have created an edit text with the ideas sign up password and input type as text password that will make the password in a strict format then we have a button whose ID is signed up button and then lastly we have a text view whose ID is login redirect where I have wrote the text as already registered login and that's it this is how it looks I'll slowly scroll the code so that you can copy paste it all right now let's move to activity login we will use the same code with few changes so copy the entire code and paste it in activity login change the background then context here change the idl's login username and here change the ID as login password then change the button ideas login button and text as login then change the text view ID and sign up redirect and text as not yet registered sign up lastly change all the associate constraint IDs according to the new IDs and then that's it this is how it looks then we need to redesign activity main.xml so when the user will successfully logged in then he will enter into activity main so let me quickly redesign it and done we are done with all the designing part and now it's time for logic close all the damps now listen to me carefully as I previously sent sqlite involves queries right queries ends in instruction to insert the data to delete the data to read the data and to update the data coded we will write all these queries and function in a class called as database helper so right click on package then new and then quickly in class write the name as database helper this is very important might look little complicated but trust me SQL queries are very simple so I'll divide it in six steps okay let me give you a quick overview so in Step number one we will declare all the constants then in Step number two we will extend sqlite open helper then in Step number three we will create table then in Step number four we will drop the table then in Step number five we will insert the new user and finally in last step that is Step number six we will read the user and that's it let's do a step by step so step number one is to declare all the constants let me quickly write it and then see in Court Lane companion object is used to define a Singleton object within a class now what is Singleton so basically a Singleton ensure that a class has only one instance and now what is instance basically the object so this object can be accessed using the class name itself without creating an instance of the class then inside companion object we have all the constant value means all of them are fixed values and cannot be changed so first is database name that is user database then database version as one then table elements data and then we have three columns that is ID username and password now step number two is to extend sqlite open helper so first of all I will pass a parameter as context that will provide access to various resources and services of the application then I will call sqlite open helper Constructor it is responsible for managing database creation now inside it we will have four parameters first is context then second one is database name then third one is cursor Factory parameter which we don't require so I'll keep it as null and finally last one is database version inside sqlite openhelper we will override the oncreate and on upgrade methods that is our step number three and four so Implement both the members now step number three we will create a table using query so first let me write it and then now see I have created a variable called as create table query and initialize it with a query this is how the query goes create table accessing the table name then inside the brackets we will mention all the columns so First Column is column ID then will be an integer and primary key Auto increment primary key is a concept of SQL in simple word it means to have a unique value for each item basically it will act as a serial number then next column is username and last column is password that's how our table is created with three columns then after that we have executed the query using database dot execute SQL and inside that I have mentioned the create table query variable and that's it now step number four is on upgrade function we call this matter when the database needs to be upgraded typically when the database version is increased okay so let me quickly write the code and then I have renamed it to make it more understandable now see we have used create table query to create a table so over here we have used draft table query to drop the table means to remove the table from the database so the query goes like Drop table if exist whatever the table name is then using execute SQL we have executed the query once the table is dropped then a new table will be created using oncreate method and done that's it now step number five is to create insert user function so let me quickly write it and then now see we have created a function named as insert user that takes two parameter as username and password okay long represents the written value means that the function will return the row ID of the newly inserted record then we have created a values variable that contains the content values which is a key value for inserting or updating the records in sqlite and then apply function basically set the values then inside it we have set values associated with its column using put so basically username is stored in column username and password is stored in column password simple right then we have created a DB variable that calls writable database property this property is a part of sqlite open helper class which allows data modification then lastly this line inserts the values specified by the table name and the values variable which we created before and then finally it will return the row ID of newly inserted item and that's it now step number six and our last step in database helper class is to read database so let me quickly write it and done here I have mentioned two parameters as username and password then Boolean is a written value means it will be either true or false so if the user is present in the database it will return true otherwise false got it then I have created a variable as DB that calls readable database property that basically reads or view the data see we will call insert user function in sign up activity and read user function in login activity got it so this line specify the condition for the query where the username and password column must match the provided username and password respectively then this line creates an array of selection arguments which is used to replace the question marks with username and password then this query retrieves the table name selection and selection arguments all of this are set to null because we don't require it so basically selection and selection argument should match and if it does match then it will return a cursor now what is cursor it is basically a concept in SQL when cursor act as a pointer to the rows in the database then we have created another variable as user exist now see if the sign up is successful then database usually assign positive integers like 1 2 3 but if the sign up is failed then in that case database assign negative integers like minus 1 minus 2. so the condition says as cursor count is greater than 0 means cursor is at positive integer then it will be considered as user exist then the cursor will be closed and finally it will return Boolean value as if the user exists then it will return true otherwise if the user does not exist then it will return false simple right and that's it now simply we have to call this function in sign up and login activity so go to sign up activity first let me quickly set up binding and then then as I said we'll be calling database function over here so for that we will require to declare an initialize database helper class so let me quickly do that and then now we will create a sangnam database function that will take username and password from us so let me quickly write it and done now see we have created a variable as inserted row ID that basically calls the insert user method from database helper class it will take two parameters as username and password now as I previously said that if the row ID is positive integer that means sign up is successful but if the row ID is negative integer that means sign up is unsuccessful that is what exactly we have used over here so if inserted row ID is not equal to -1 long that means inserted row ID is positive which indicates an appears successful hence I have wrote a toast as sign up successful and with the help of intent I have redirected user to login screen else if the sign up is unsuccessful means the inserted row ID is in negative integer then a toast will appear as sign up failed got it now all of this will happen when user will click on sign up button so inside the oncreate method write binding dot signed up button dot set on click listener and inside it take the inputs from user through edit text so let me quickly write it and done once we have both the input values we will call the sign up database function and inside that we will pass the sign up username and sign up password as arguments basically it will take both the input values and use a insert user method it will store them in the table then lastly we'll create an intent on login redirect so set on click listener on it and inside that create an intent that will redirect the user from sign up screen to login screen and done that's it similarly we will do it in login activity as well so let's do it quickly go to login activity first I'll set up binding and done then declare and initialize the database helper task and done then over here we will create a login database function so let me quickly write it and done now see we have a variable name called as user exist that basically calls the read user function from the database helper class it takes two parameter as username and password so if the user exists then login and successful else if the user does not exist then it will throw a toast as login failed simple right then we will call this function in login button so set on click listener on it then inside it I'll take both the inputs from the user through edit text so let me quickly write it [Music] and done once I have both the input values I'll call login database function and inside it I'll pass login username and login password as arguments so basically it will take the input values and with the help of read user function it will check if the credentials is present in the database or not lastly I'll set on click listener on sign up redirect so let me quickly write it and done it will redirect the user from login screen to sign up screen and that's it we are done with the coding part now let's run the app this is how it looks so the first screen is login screen as we have not registered yet so go to sign up screen this is where I will create username and password and done I'll click on sign up button and it says sign up is successful also it automatically redirected us to login screen here I will write the same credentials then I'll click on login button and see we are successfully logged in perfect now how do we access sqlite database first of all make sure that you have installed simple sqlite browser plugin I have already installed it so now when the emulator is in running mode then go to device file explorer here go to data then again data then find your app name here it is login sign up SQL then go to database and inside it we have a database name that is user database click on it select your table name as data and see with the help of SQL Lite plugin we can view the table this is a table with three columns in it ID username and password all of them will be stored here got it so yeah that is it for the video if you are new to this Channel please consider subscribing to my channel and I'll see you in the next video [Music] foreign
Info
Channel: Android Knowledge
Views: 5,196
Rating: undefined out of 5
Keywords: ogin screen in android studio, android sqlite, simple login app android studio, login and register android studio, android studio sqlite, login and register page in android using database, login and registration in android, login and registration form in android studio, login and register android studio firebase, how to create login page in android studio, how to login and register in android app, registraion and login form in android, android studio sqlite database, kotlin, design
Id: zz659HPTe6M
Channel Id: undefined
Length: 34min 18sec (2058 seconds)
Published: Mon Jul 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.