Hello friends, today I am going to tell
you how can we create a login and register app using SQlite database which
is stored locally in your phone. So let us begin First, we will launch Android studio... and select "start a new project" Select Empty activity and click Next. Provide a name to your project "LoginSqlite", set other parameters... So you can see MainActivity.java has opened, and there is an activity_main.xml which is our UI file Go to text and what we will do we will delete the hello world text which is the default text.
and will change the layout to relative layout we will give some padding here "10dp" and then in the layout first we will make an edittext. Width is 'match_parent'
height is wrap_content We have to give an ID. The ID will
be username. We can give a hint parameter, that is username now we can
give some margin. So layout margin top = "50dp". and copy this edittext and we'll create
another edittext. the ID we'll change it to password and since its a relative
layout, we have to define the relationship with other elements, so we will define
layout_below the ID of "@+id/username" then we'll copy this edittext and we
will create ... another edittext box below that and we will change the ID as "@+id/repassword" which is basically for retype password and then this should be layout_below "@+id/password". We have to change the... hint to the second edittext and that should be "password" and then for the third edit text box, it
should be "retype password". Now we have to create a button, width
should be match_parent and height is wrap_content. The ID of the button, we will
assign"@+id/buttonsignup" then we have to write a text to the button, so we will write
'register'. Then margin_top we will define as 50dp. Then it should be below the third edittext... so we will define layout_below "@+id//repassword". We'll copy this portion and create
another button. The ID of this button we''ll change it to @+id/btnsigni" and it
should be below "@+id/btnsignup". The text we will change here as Existing
user go to sign-in page". That's it friends we will go to design view and we'll see that
all the elements have appeared properly. There are three edit text box and two
buttons. Now let us go to mainactivity.java so here in MainActivity.Java
we have to define some variables, Edittext... username, password and repassword. now we will define variables for button
so btnsignup and btnsignin. Now inside the onCreate method we will initialize
these variables. We'll write username equal to {edit text} findViewById R.id. .... we have to write here the ID of the edittext box
which is 'username' similarly for password, you will write
password equal to edittext findViewById R.id. password and then third edittext box repassword equal to edit text find you by ID R.id.repassword. Now we will initialize btnignup equal {Button} findViewById R.id. btnsignup then signin = {Button} find view by ID R.id.btnsignin Now we will create listeners for
the buttons signup.setOnClick listener the option comes automatically
then write new view then select the option
so this is our listener for the button inside the on click method we can write
what should happen with the when the person clicks on the button then down below we will create another
listener for our second button which is sign-in button so sign in dot at
onclicklistener new view on click listener now go to the left panel create new Java
class the doubtless will create here will be DB helper which will basically
contain the code or our SQLite database you can see public class DB helper has
opened we will write public class DB helper extents as you like to open
helper inside that we have to create two methods first method is on create method
and second method is on upgrade method above T on create method will read
public static final string DB name equals login dot DB under the double
quotes so this will be our name of the database which will be created inside
your Android device now we'll create a constructor here so we'll select the
first option and then press ok these options will delete we will also delete
now level and now in the super context then you'll write login DB then we will
write null and version we will write one so this will create a database in login.db.
now in the oncreate method will change this SQLite database myDB and then here
also will change the name SQLite database to my DB now in the oncreate method inside the DB
helper class we will write my DB dot exacSQL which will execute an SQL
query we'll write under the quotes "create table users now inside the
bracket you will write the column names username text which is a primary key
then second column should be password type text. So what this query will do
this query will create a table users which will have two columns first column
username and second column password inside the onUpgrade method we will
write myDB.execSQL "drop table if exists users". Now we will create another
method public Boolean insertData(string username, String password). So this
method we are creating basically to insert the data. I will rescue my
database myDB = this. getWritableDatabase then we will
write content values = new content values; then contentvalues.put (username which is our column name first column name username that is the
string. Now copy and paste that below and change the column name as password. The string variable is... we will also change it to password long results = myDB.insert so now we'll insert the data null, content values so when we write
content values here all the values will be put inside the database. Now we'll
check if result = -1 ; return false. So if the insertion is not possible or this
insertion has failed this will return false else if result is not -1; the -1 basically indicates failure; so we'll write else return true.
So this function was for the insertion of the data. Now we will create next
function public Boolean checkusername. we'll pass an string string username; this
function will check whether the user exists in the table already or not.
We will have to write SQliteDatabase myDB = this.getWritableDatabase();
Cursor cursor... here for cursor, we have to import the method. so import the class by alt + enter. You'll see the cursor class is imported cursor cursor equal to
myDB.rawQuery ("select * from users where username = ?". And outside this , new string large brackets inside the curly bracket
we will read {username} and then we will let if(cursor.getCount() > 0) that means if user exists we will return true; else we
will return false. Now we will create another function public Boolean checkusernamepassword(); first argument should be string
username second is the string password inside that well write SQLiteDatabase
myDB = this.getWritableDatabase(); then we'll write in the same
manner Cursor cursor = myDB. rawQuery(). Here we'll write "select star
from users where username = ? and password = ? , new string... now here inside the curly brackets we will write two
strings username , password then we'll check if(cursor.getCount>0) return true; else return false. That's all for our DBHelper.class. Now create a new activity which is login activity. Name it LoginActivity then press next. And you can see login activity . Java is also open so
now we are two activities one is main activity and why that is the login
activity which will access this DBHelper.class when you go to the layout XML file of
the login activity activity_login.xml we'll copy the two edit text boxes and
paste inside the login activity .XML We will change the layout from
constraint to relative layout. will change the IDs... just add 1 in
username1 and password1.... just to differentiate. Then we'll copy the button
also, from activity_main.xml and basically this sign-in button we will copy
this sign-in button and paste below the second edittext text box we will define
below which it should come it should be below the password so we'll write a
layout below = @+id/password and then text we will change is sign in. the ID we
will change btnsignin1. we have to correct this ID also. we can define some padding that's it it
is come nicely insert the login activity also we will
define two variables edit text username password and we'll define button button
login and we will now initialize them username equal to edit text find view by
ID R.id.username1. Password = edittext find view by ID(R.id.password1) one then btnlogin = button find view by ID R.id.btnsign the down below we will create a listener
button login dot setOnClicklistener new view.OnClickListener coming to mainactivity inside the first
create function for our sign-in button in the main activity will write intent
intent we have to import the intent class
intent intent equals to new intent get application context , login activity
which we have just created so when we place this button it will redirect to
the login page we'll right here start activity intent now now let us define
now let us define a variable DBHelper DB and we'll initialize DB = new DBHelper context this and now under the onclick method we will create two
strings, string user equal to username .gettext().tostring(). basically
whatever user enters in the edit text box will be stored in this string user
then string pass equal to password dot get text door to string then the string
repass equal to re password.gettext(). dot tostring(). Now once we have created
three strings we will write if user. equals "" or password again "" or repass.equals " ". so basically it
is checking any of the edittext box is empty. so if any of the box is empty it
will show a Toast message. We will create a toast message here define the text as
please enter all fields we can write the toast message as per your choice else that
means if all the edit text boxes have some values then it will go for checking
whether user exists or not will write if... So it will go for whether
these two passwords match or not will write else if pass.equals repass that
means we have a username and we have two passwords matching, we will write boolean
check user equal to then we'll check in the database that whether the user
already exists or not DB.checkusername we will pass the
string user. If check user = false check is are equal to positive two if
user does not exist we can insert the data so we will write boolean insert
equals DB.insert data which is a function
we've created in the body BL per class user , password this function will
execute the insertion of the data with the given name user and possible if
insert equals to true that means if the database has been written we will show
toast message and we will write registered successfully" now we will
create another activity... we'll call it a home activity basically when the
registration is successful you will let user to move to one page which will be
for our other activities which will be a home activity so now home activity has
been created so we will write intent intent = new
intent getapplicationcontext, HomeActivity.class
start activity (intent) so if data has been inserted we will switch to HomeActivity
but if the insert is not true so we write else now here again we will get a
toast saying registration failed". If check user has returned true we will write else if
the check user has returned true we will say user already exists
please sign in else we show a toast message here
"passwords not matching" so if passwords not matching it will not
allow user to enter the data now go to the login activity now we will define DBHelper. Let is write DB then initialize DB = new DBHelper context (this) and inside the button listener btnlogin listener
store the variables inside the string string user = username.get
text . to string and the string pass = password .get text(). tostring() in
this way only to detects we will write if user dot equals blank will again
check here whether the fields are empty so we'll write pass dot equals blank
under the codes so you anyone is empty we will show our
toast message please enter all the fields which we showed in the main
activity else if the strings are not empty we will write Boolean check
user pass equal to DB.check user name password which requires two arguments
one is user string and pass string now we will write if check user pass =
true that means if user password exists with the given information we will say
here using a toast message sign-in successfull. we will allow user to sign in
we can send the user to home activity we write intent intent equal to again
here we have to import the intent new intent getapplicationcontext,
Homeactivity .class start activity intent if username and password does not
match with the stored values you will show toast message "invalid credentials and that's it , our program is ready now will run so here I am running this in
my mirrored my phone... so it is installing Program is launched. yeah so here we can
see three edit boxes and two buttons we will write username allcoding and
password just one two three four five six and we will write another one two
three four five six repress register we see Oh activity with them toast message
now if we enter another username it again it goes to the home activity that
means entry has been created now if we make if you change the password and
we write allcoding so let us write username allcoding password as 1 2 3 4 5
6 again repeat the password 1 2 3 4 5 6 register username already exists so if
we attempt writing the same values it rejects. Now go to the next page by
clicking on the button here in the login page we will write our username
which we here register allcoding write the password and when we press sign in
gives a message signing successful we go back we use the second username the
gains and in successful if you give one another username which is not registered
it says invalid credentials now I tell you how to see the database where we
have stored all these values so let us click the device monitor which is
located on your it will open a device file explorer' there you can locate data
then data inside the data your to search for your package name inside the package
name search for the database and then database login dot DB you can see will
save us in our location from where we can access now here I have a DB browser for SQLite. double-click on it and open the database select a database which is
stored in your PC this opens it shows the two entries one for metadata and
second for users it has two columns username password which we define in our
program browse the table by right-clicking on it and you can see two
entries all coding and non-coding - all coding one with the passwords these are
the entries which we can see these are from our entry device that's it guys I
hope you must have learned something from it and you will find it useful
please subscribe to our Channel we'll bring other videos like this, don't
forget to like the video. thank you