how to create login page in android studio using sqlite database
In this Article today learn how to create login page in android studio
using sqlite database. In this
tutorial, we show you how to create Android Login Example with SQLite Database
using Android Studio. This Application has two screens, first one is login
screen where we can just login with our credentials. Second screen is Welcome
screen after we login successfully.
follow the steps how to create login page in android studio using sqlite database.
1.MainActivity.java:
package com.akash.androidsqlite;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btnLogin;
EditText edtUsername;
EditText edtPassword;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = (Button) findViewById(R.id.btnLogin);
edtUsername = (EditText) findViewById(R.id.edtUsername);
edtPassword = (EditText) findViewById(R.id.edtPassword);
databaseHelper = new DatabaseHelper(MainActivity.this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isExist = databaseHelper.checkUserExist(edtUsername.getText().toString(), edtPassword.getText().toString());
if(isExist){
Intent intent = new Intent(MainActivity.this, Welcome.class);
intent.putExtra("username", edtUsername.getText().toString());
startActivity(intent);
} else {
edtPassword.setText(null);
Toast.makeText(MainActivity.this, "Login failed. Invalid username or password.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
2.DatabaseHelper.java:
package com.akash.androidsqlite;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;
private final Context context;
SQLiteDatabase db;
private static final String DATABASE_PATH = "/data/data/com.jackrutorial.loginwithsqlitedbexample/databases/";
private final String USER_TABLE = "user";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
createDb();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDb(){
boolean dbExist = checkDbExist();
if(!dbExist){
this.getReadableDatabase();
copyDatabase();
}
}
private boolean checkDbExist(){
SQLiteDatabase sqLiteDatabase = null;
try{
String path = DATABASE_PATH + DATABASE_NAME;
sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception ex){
}
if(sqLiteDatabase != null){
sqLiteDatabase.close();
return true;
}
return false;
}
private void copyDatabase(){
try {
InputStream inputStream = context.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0){
outputStream.write(b, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private SQLiteDatabase openDatabase(){
String path = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
return db;
}
public void close(){
if(db != null){
db.close();
}
}
public boolean checkUserExist(String username, String password){
String[] columns = {"username"};
db = openDatabase();
String selection = "username=? and password = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(USER_TABLE, columns, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
close();
if(count > 0){
return true;
} else {
return false;
}
}
}
3.Welcome.java:
package com.akash.androidsqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Welcome extends AppCompatActivity {
TextView helloUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
helloUser = (TextView) findViewById(R.id.helloUser);
Bundle extras = getIntent().getExtras();
String username = null;
if(extras != null){
username = extras.getString("username");
helloUser.setText("Welcome " + username);
}
}
}
4.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: "
/>
<EditText
android:id="@+id/edtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password: "
/>
<EditText
android:id="@+id/edtPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
5.activity_welcome.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Welcome">
<TextView
android:id="@+id/helloUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</android.support.constraint.ConstraintLayout>
6.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.androidsqlite">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Welcome"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment