TechSupportNepTechSupportNep - Programming & Tech

  • Home
  • C#.NET
  • PHP
  • JAVA
  • Python
  • ASP.NET
  • VB.NET
  • Android
  • C
  • Tech
    • Tech News
  • Patreoner
TechSupportNep
  • Home
  • Programming
  • Android
  • Android Login and Register with SQLite Database Tutorial [With Source Code]
Android

Android Login and Register with SQLite Database Tutorial [With Source Code]

By Er. Ran Bahadur B.K. Last updated Aug 15, 2021
25 11,170
Share

To create login and Registraion form in android, we need a class which manage our SQLite Database and table.

For this create new project and then add one class by right click the mainactivity>new>class. Then give the name of the class as DatabaseHelper. Now extends this class with SQLiteOpenHelper. Then implement the methods and constructor of SQLiteOpenHelper. For this, focus the cursur in the class and press alt+enter.

Now write the following code to design schema.

Schema define how database is organized. it is just like a SQL statements we use to create database and table.


public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="register.db";
public static final String TABLE_NAME="registeration";
public static final String COL_1="ID";
public static final String COL_2="FirstName";
public static final String COL_3="LastName";
public static final String COL_4="Password";
public static final String COL_5="Email";
public static final String COL_6="Phone";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME); //Drop older table if exists
onCreate(db);
}
}

Now design the Registraion form in activity_main.xml file as show in video. watch the video for this purpose. After completing the design part go back to MainActivity.java and write the following code.

1. Declare the button and textfieldas and sqlitedatabase and sqliteopenhelper inside the class as:


public class MainActivity extends AppCompatActivity {
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Button _btnreg, _btnlogin;
EditText _txtfanme, _txtlname, _txtpass, _txtemail, _txtphone;

2. Inside the onCreate method initialize all the button, textfield and call the databasehelper class as:


openHelper = new DataBaseHelper(this);
_txtfname = (EditText)findViewById(R.id.txtfname);
_txtlname = (EditText)findViewById(R.id.txtlname);
_txtpass = (EditText)findViewById(R.id.txtpass);
_txtemail = (EditText)findViewById(R.id.txtemail);
_txtphone = (EditText)findViewById(R.id.txtphone);
_btnlogin=(Button)findViewById(R.id.btnlogin);
_btnreg=(Button)findViewById(R.id.btnreg);

3. Create the listening event of _btnreg as follows and write the code inside that listening event of the button.


_btnreg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db=openHelper.getWritableDatabase();
String fname=_txtfanme.getText().toString();
String lname=_txtlname.getText().toString();
String pass=_txtpass.getText().toString();
String email=_txtemail.getText().toString();
String phone=_txtphone.getText().toString();
insertdata(fname, lname,pass,email,phone);
Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show();

}
});

4. Now create one method having the name insertdata. as follows and write the following code inside that method.


public void insertdata(String fname, String lname, String pass, String email, String phone){
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_2, fname);
contentValues.put(DatabaseHelper.COL_3, lname);
contentValues.put(DatabaseHelper.COL_4, pass);
contentValues.put(DatabaseHelper.COL_5, email);
contentValues.put(DatabaseHelper.COL_6, phone);
long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
}
});

This parts complete the registration part now we need to move on towards the login part.

1. Create the listening event of the button _btnlogin as follow and write the code as:


_btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, login.class);
startActivity(intent);
}
});
}
});

2.Add one empty activity by right clicking the MainActivity.java file as MainActivity>new>Activity>EmptyActivity and give the name of this activity whatever you want but i am giving the name login.java.

3. Go to xml file of this recently added(login.java) activity xml file and desing the login form see the video for this.

4. Go back to the login.java and write the following code.

a. Declare the textfield, button, sqlitedatabase and sqliteopenhelper class inside the login class as:


SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Cursor cursor;
Button _btnLogin;
EditText _txtEmail, _txtPass;

b. Initialize the button and textfield and call the databasehelper class inside the onCreate methods as:


_txtEmail=(EditText)findViewById(R.id.txtEmail);
_txtPass=(EditText)findViewById(R.id.txtPass);
_btnLogin=(Button)findViewById(R.id.btnLogin);
openHelper=new DataBaseHelper(this);
db = openHelper.getReadableDatabase();

c. Create the listening event of the button and write the following code inside the listening event of the button.


_btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = _txtEmail.getText().toString();
String pass = _txtPass.getText().toString();

cursor = db.rawQuery("SELECT *FROM " + DataBaseHelper.TABLE_NAME + " WHERE " + DataBaseHelper.COL_5 + "=? AND " + DataBaseHelper.COL_4 + "=?", new String[]{email, pass});
if (cursor != null) {
if (cursor.getCount() > 0) {
Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_SHORT).show();
}
}
}
});

That’s it guyz.

if this article help you stay connect with us and don’t forget to watch the following tutorial video because you will not understand this whole tutorial without watching this video.

 

 

Continue Reading
android databaseandroid database appandroid database exampleandroid database tutorialandroid login and register with sqlite source codeandroid login with databaseandroid register appandroid sqliteandroid studio databaseandroid studio sqliteandroid studio sqlite databasedatabase androiddatabase in android studiohow to create database in android applicationhow to create login and registration form in androidlogin and registraion form in android using sqliteregistraion and login form in androidregistraion and login form in android using sqlite databasesqlite create databasesqlite database in androidtechsupportnepal
25 11,170
Share FacebookTwitterReddItWhatsAppPinterestEmail
Er. Ran Bahadur B.K.60 posts 56 comments

Ran Bahadur B.K. is the owner of TechSupportNep who is working with the aim of providing the quality Technology content. He is creating Tech and Programming videos, you can see all them in his YouTube Channel(https://www.youtube.com/TechSupportNep). He is also working with some company.

Prev Post

How to Send Free SMS From PHP Using SMS API?[With Source Code]

Next Post

How to Insert, Delete, Update and View Data From MySQL Database using C#.NET? [With Source Code]

Show Comments (25)
Help Needed

This website is free of annoying ads. We want to keep it like this. You can help with your support:

Donate!

The need for Support! Why to Support?

If you need your own website? contact me!
erranbahadurbk
Fiverr
Seller
Hello there,This is me Ran Bahadur B.K., A graduated computer engineer. I have 3+ years of work experience in wordpress design and development. I have strong knowledge of HTML, CSS and PHP which are the three ingredients of web development. Currently i am working with the software company so i also have experience in desktop apps and mobile apps development.

Latest

Python

How to Create Login Application in Python using SQLite Database?

How to use View Binding in Android?

Windows 10 Build 1909 Features that you should know.

How to Create a Complete Login and Forgot Password System in java ?

Prev Next 1 of 15
  • About us
  • Contact us
  • Privacy Policy
  • Disclaimer
© 2022 All Rights Reserved.
Website By: Ran Bahadur B.K.