Updated 2026
Android SQLite Login & Register App with Full CRUD Operations
A complete, production-ready Android project using SQLite database, SharedPreferences session management, and input validation — with full Java source code and XML layouts.
SQLite is a lightweight, serverless relational database built directly into Android. It is the ideal choice for storing structured user data locally — without requiring an internet connection. This tutorial walks you through a complete Login & Register Android application backed by SQLite, covering all four CRUD operations: Create (register), Read (login / profile), Update (edit profile), and Delete (implicit via session logout).
What You Will Build
🔐Login Screen
📝Register Screen
👤User Profile Screen
✏️Update User Screen
🗄️SQLite Database
💾Session with SharedPrefs
✅Email Validation
🔄Auto-login on relaunch
App Screenshots
Resource Files
values/
color.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3b5998</color>
<color name="colorPrimaryDark">#3b5998</color>
<color name="colorAccent">#3b5998</color>
<color name="lavender">#3b5998</color>
<color name="white">#FFFFFF</color>
<color name="login_button">#D81B60</color>
</resources>
strings.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Login</string>
<string name="email">Email</string>
<string name="login">Login</string>
<string name="name">Name</string>
<string name="password">Password</string>
<string name="phone">Phone number</string>
<string name="register">Register</string>
<string name="user_login">User Login</string>
<string name="please_enter_name">Please enter name</string>
<string name="please_enter_email">Please enter email</string>
<string name="please_enter_password">Please enter password</string>
<string name="please_enter_phone">Please enter phone number</string>
<string name="loading">Loading...</string>
<string name="ok">OK</string>
<string name="register_success">Registration successful</string>
<string name="email_id_use">Email address already in use</string>
<string name="fail">Login Failed</string>
<string name="update">Update</string>
<string name="logout">Logout</string>
<string name="update_user">Update User</string>
</resources>
styles.xml
XML
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
drawable/
edittext_bg.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/white" />
<corners android:radius="20dp" />
</shape>
</item>
</selector>
login_button_bg.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="40dp" />
</shape>
</item>
</selector>
XML Layout Files
activity_login.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lavender"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/lavender"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.Login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText_email_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<EditText
android:id="@+id/editText_password_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/password"
android:inputType="textPassword"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button_login"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_button_bg"
android:text="@string/login"
android:textColor="@color/login_button"
android:textStyle="bold" />
<Button
android:id="@+id/button_reg_login"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_button_bg"
android:text="@string/register"
android:textColor="@color/login_button"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
activity_register.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lavender"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.Register">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/register"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText_name_reg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/name"
android:inputType="text"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<EditText
android:id="@+id/editText_email_reg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<EditText
android:id="@+id/editText_password_reg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/password"
android:inputType="textPassword"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<EditText
android:id="@+id/editText_phone_reg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/edittext_bg"
android:hint="@string/phone"
android:inputType="phone"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textCursorDrawable="@null" />
<Button
android:id="@+id/button_reg"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
android:background="@drawable/login_button_bg"
android:text="@string/register"
android:textColor="@color/login_button"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
activity_main.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
tools:context=".Activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="10dp"
android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textSize="18sp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:text=":-" android:textSize="18sp" />
<TextView android:id="@+id/textView_name_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/email" android:textSize="18sp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:text=":-" android:textSize="18sp" />
<TextView android:id="@+id/textView_email_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phone" android:textSize="18sp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:text=":-" android:textSize="18sp" />
<TextView android:id="@+id/textView_phone_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal">
<Button android:id="@+id/button_update_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/update" />
<Button android:id="@+id/button_logout_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:text="@string/logout" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
activity_update_user.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lavender"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.UpdateUser">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/update_user"
android:textColor="@color/white"
android:textSize="32sp"
android:textStyle="bold" />
<EditText android:id="@+id/editText_name_update" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginTop="50dp" android:layout_marginEnd="20dp" android:background="@drawable/edittext_bg" android:hint="@string/name" android:inputType="text" android:paddingStart="10dp" android:paddingEnd="10dp" android:singleLine="true" android:textColor="@color/white" android:textColorHint="@color/white" android:textCursorDrawable="@null" />
<EditText android:id="@+id/editText_email_update" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_marginEnd="20dp" android:background="@drawable/edittext_bg" android:hint="@string/email" android:inputType="textEmailAddress" android:paddingStart="10dp" android:paddingEnd="10dp" android:singleLine="true" android:textColor="@color/white" android:textColorHint="@color/white" android:textCursorDrawable="@null" />
<EditText android:id="@+id/editText_password_update" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_marginEnd="20dp" android:background="@drawable/edittext_bg" android:hint="@string/password" android:inputType="textPassword" android:paddingStart="10dp" android:paddingEnd="10dp" android:singleLine="true" android:textColor="@color/white" android:textColorHint="@color/white" android:textCursorDrawable="@null" />
<EditText android:id="@+id/editText_phone_update" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_marginEnd="20dp" android:background="@drawable/edittext_bg" android:hint="@string/phone" android:inputType="phone" android:paddingStart="10dp" android:paddingEnd="10dp" android:singleLine="true" android:textColor="@color/white" android:textColorHint="@color/white" android:textCursorDrawable="@null" />
<Button
android:id="@+id/button_update"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
android:background="@drawable/login_button_bg"
android:text="@string/update"
android:textColor="@color/login_button"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
AndroidManifest.xml
AndroidManifest.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.databaseloginregister">
<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=".Activity.Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity.Register" />
<activity android:name=".Activity.UpdateUser" />
<activity android:name=".Activity.MainActivity" />
</application>
</manifest>
Java Source Code
Login.java
Java
package com.app.databaseloginregister.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import com.app.databaseloginregister.DataBase.DatabaseHandler;
import com.app.databaseloginregister.R;
import com.app.databaseloginregister.Util.Method;
public class Login extends AppCompatActivity {
private Method method;
private DatabaseHandler db;
private String email, password;
private EditText editText_email, editText_password;
private Button button_reg, button_login;
private InputMethodManager imm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
method = new Method(Login.this);
db = new DatabaseHandler(Login.this);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Auto-login if session exists
if (method.pref.getBoolean(method.pref_login, false)) {
String id = method.pref.getString(method.profileId, null);
startActivity(new Intent(Login.this, MainActivity.class).putExtra("id", id));
finishAffinity();
}
editText_email = findViewById(R.id.editText_email_login);
editText_password = findViewById(R.id.editText_password_login);
button_login = findViewById(R.id.button_login);
button_reg = findViewById(R.id.button_reg_login);
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
email = editText_email.getText().toString().trim();
password = editText_password.getText().toString();
editText_email.clearFocus();
editText_password.clearFocus();
imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
login();
}
});
button_reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Login.this, Register.class));
}
});
}
private void login() {
editText_email.setError(null);
editText_password.setError(null);
if (!isValidMail(email) || email.isEmpty()) {
editText_email.requestFocus();
editText_email.setError(getString(R.string.please_enter_email));
} else if (password.isEmpty()) {
editText_password.requestFocus();
editText_password.setError(getString(R.string.please_enter_password));
} else {
if (!db.checkLogin(email, password)) {
String id = db.getUserId(email);
if (id != null) {
method.editor.putBoolean(method.pref_login, true);
method.editor.putString(method.profileId, id);
method.editor.apply();
startActivity(new Intent(Login.this, MainActivity.class).putExtra("id", id));
finishAffinity();
}
} else {
method.alertBox(getString(R.string.fail));
}
}
}
private boolean isValidMail(String email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}
Register.java
Java
package com.app.databaseloginregister.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import com.app.databaseloginregister.DataBase.DatabaseHandler;
import com.app.databaseloginregister.R;
import com.app.databaseloginregister.Util.Method;
public class Register extends AppCompatActivity {
private Method method;
private DatabaseHandler db;
private Button button_register;
private InputMethodManager imm;
private String name, email, password, phoneNo;
private EditText editText_name, editText_email, editText_password, editText_phoneNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
method = new Method(Register.this);
db = new DatabaseHandler(Register.this);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
editText_name = findViewById(R.id.editText_name_reg);
editText_email = findViewById(R.id.editText_email_reg);
editText_password= findViewById(R.id.editText_password_reg);
editText_phoneNo = findViewById(R.id.editText_phone_reg);
button_register = findViewById(R.id.button_reg);
button_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = editText_name.getText().toString().trim();
email = editText_email.getText().toString().trim();
password= editText_password.getText().toString();
phoneNo = editText_phoneNo.getText().toString().trim();
editText_name.clearFocus();
editText_email.clearFocus();
editText_password.clearFocus();
editText_phoneNo.clearFocus();
imm.hideSoftInputFromWindow(editText_name.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_phoneNo.getWindowToken(), 0);
form();
}
});
}
private boolean isValidMail(String email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void form() {
editText_name.setError(null);
editText_email.setError(null);
editText_password.setError(null);
editText_phoneNo.setError(null);
if (name.isEmpty()) {
editText_name.requestFocus();
editText_name.setError(getString(R.string.please_enter_name));
} else if (!isValidMail(email) || email.isEmpty()) {
editText_email.requestFocus();
editText_email.setError(getString(R.string.please_enter_email));
} else if (password.isEmpty()) {
editText_password.requestFocus();
editText_password.setError(getString(R.string.please_enter_password));
} else if (phoneNo.isEmpty()) {
editText_phoneNo.requestFocus();
editText_phoneNo.setError(getString(R.string.please_enter_phone));
} else {
if (db.checkEmail(email)) {
db.addUser(name, email, password, phoneNo);
editText_name.setText("");
editText_email.setText("");
editText_password.setText("");
editText_phoneNo.setText("");
method.alertBox(getString(R.string.register_success));
startActivity(new Intent(Register.this, Login.class));
} else {
method.alertBox(getString(R.string.email_id_use));
}
}
}
}
MainActivity.java
Java
package com.app.databaseloginregister.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.app.databaseloginregister.DataBase.DatabaseHandler;
import com.app.databaseloginregister.Item.UserList;
import com.app.databaseloginregister.R;
import com.app.databaseloginregister.Util.Method;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String id;
private Method method;
private DatabaseHandler db;
private List<UserList> userLists;
private Button button_update, button_logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id = getIntent().getStringExtra("id");
method = new Method(MainActivity.this);
userLists= new ArrayList<>();
db = new DatabaseHandler(MainActivity.this);
userLists= db.getUser(id);
TextView textView_name = findViewById(R.id.textView_name_main);
TextView textView_email = findViewById(R.id.textView_email_main);
TextView textView_phoneNo = findViewById(R.id.textView_phone_main);
button_update = findViewById(R.id.button_update_main);
button_logout = findViewById(R.id.button_logout_main);
if (!userLists.isEmpty()) {
textView_name.setText(userLists.get(0).getName());
textView_email.setText(userLists.get(0).getEmail());
textView_phoneNo.setText(userLists.get(0).getPhone());
}
button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, UpdateUser.class)
.putExtra("id", id)
.putExtra("name", userLists.get(0).getName())
.putExtra("email", userLists.get(0).getEmail())
.putExtra("phone", userLists.get(0).getPhone()));
}
});
button_logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
method.editor.putBoolean(method.pref_login, false);
method.editor.apply();
startActivity(new Intent(MainActivity.this, Login.class));
finishAffinity();
}
});
}
}
UpdateUser.java
Java
package com.app.databaseloginregister.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import com.app.databaseloginregister.DataBase.DatabaseHandler;
import com.app.databaseloginregister.R;
import com.app.databaseloginregister.Util.Method;
public class UpdateUser extends AppCompatActivity {
private Method method;
private DatabaseHandler db;
private Button button_update;
private InputMethodManager imm;
private String id, name, email, password, phoneNo;
private EditText editText_name, editText_email, editText_password, editText_phoneNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_user);
method = new Method(UpdateUser.this);
db = new DatabaseHandler(UpdateUser.this);
id = getIntent().getStringExtra("id");
name = getIntent().getStringExtra("name");
email = getIntent().getStringExtra("email");
phoneNo = getIntent().getStringExtra("phone");
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
editText_name = findViewById(R.id.editText_name_update);
editText_email = findViewById(R.id.editText_email_update);
editText_password = findViewById(R.id.editText_password_update);
editText_phoneNo = findViewById(R.id.editText_phone_update);
button_update = findViewById(R.id.button_update);
editText_name.setText(name);
editText_email.setText(email);
editText_phoneNo.setText(phoneNo);
button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = editText_name.getText().toString().trim();
email = editText_email.getText().toString().trim();
password= editText_password.getText().toString();
phoneNo = editText_phoneNo.getText().toString().trim();
editText_name.clearFocus();
editText_email.clearFocus();
editText_password.clearFocus();
editText_phoneNo.clearFocus();
imm.hideSoftInputFromWindow(editText_name.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
imm.hideSoftInputFromWindow(editText_phoneNo.getWindowToken(), 0);
form();
}
});
}
private boolean isValidMail(String email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void form() {
editText_name.setError(null);
editText_email.setError(null);
editText_phoneNo.setError(null);
if (name.isEmpty()) {
editText_name.requestFocus();
editText_name.setError(getString(R.string.please_enter_name));
} else if (!isValidMail(email) || email.isEmpty()) {
editText_email.requestFocus();
editText_email.setError(getString(R.string.please_enter_email));
} else if (phoneNo.isEmpty()) {
editText_phoneNo.requestFocus();
editText_phoneNo.setError(getString(R.string.please_enter_phone));
} else {
db.updateUser(id, name, email, password, phoneNo);
startActivity(new Intent(UpdateUser.this, MainActivity.class).putExtra("id", id));
finishAffinity();
}
}
}
SQLite Database Handler
The DatabaseHandler class extends SQLiteOpenHelper and handles all CRUD operations for the user table.
| Method | Description |
|---|---|
onCreate() | Called once on first install — creates the user table with auto-increment primary key. |
onUpgrade() | Called when DATABASE_VERSION increases — drops and recreates the table. |
addUser() | Inserts a new user record (name, email, password, phone) into the database. |
getUser(id) | Returns a List<UserList> for the given user ID. |
updateUser() | Updates name, email, phone, and optionally password for the given ID. |
checkEmail() | Returns true if the email does NOT exist (safe to register). |
getUserId() | Returns the row ID string for the given email address. |
checkLogin() | Returns false if credentials match (login success — note inverted logic). |
DatabaseHandler.java
Java
package com.app.databaseloginregister.DataBase;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.app.databaseloginregister.Item.UserList;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "demo_login";
private static final String TABLE_NAME = "user";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "user_email";
private static final String KEY_PHONE_NUMBER = "phone_number";
private static final String KEY_PASSWORD = "password";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT, "
+ KEY_EMAIL + " TEXT, "
+ KEY_PHONE_NUMBER + " TEXT, "
+ KEY_PASSWORD + " TEXT"
+ ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
/** INSERT — add a new user */
public void addUser(String name, String email, String password, String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_EMAIL, email);
values.put(KEY_PHONE_NUMBER, phone);
values.put(KEY_PASSWORD, password);
db.insert(TABLE_NAME, null, values);
db.close();
}
/** READ — fetch user by ID */
public List<UserList> getUser(String id) {
List<UserList> userLists = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_ID + " = " + id;
Log.d("DB_QUERY", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
UserList list = new UserList();
list.setName(cursor.getString(1));
list.setEmail(cursor.getString(2));
list.setPhone(cursor.getString(3));
list.setPassword(cursor.getString(4));
userLists.add(list);
} while (cursor.moveToNext());
}
return userLists;
}
/** UPDATE — update user profile */
public void updateUser(String id, String name, String email, String password, String phoneNumber) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_EMAIL, email);
if (!password.isEmpty()) values.put(KEY_PASSWORD, password);
if (!phoneNumber.isEmpty()) values.put(KEY_PHONE_NUMBER, phoneNumber);
db.update(TABLE_NAME, values, KEY_ID + "=" + id, null);
}
/** Check login credentials — returns false on success */
public boolean checkLogin(String userEmail, String userPassword) {
String selectQuery = "SELECT " + KEY_ID + " FROM " + TABLE_NAME
+ " WHERE " + KEY_EMAIL + " = '" + userEmail + "'"
+ " AND " + KEY_PASSWORD + " = '" + userPassword + "'";
Log.d("DB_QUERY", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
boolean notFound = cursor.getCount() == 0;
cursor.close();
return notFound;
}
/** Check if email is available — returns true if NOT in use */
public boolean checkEmail(String userEmail) {
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_EMAIL + " =?";
SQLiteDatabase db = this.getWritableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, new String[]{userEmail});
return cursor.getCount() == 0;
}
/** Get user ID by email */
public String getUserId(String userEmail) {
String selectQuery = "SELECT * FROM " + TABLE_NAME
+ " WHERE " + KEY_EMAIL + " = '" + userEmail + "'";
SQLiteDatabase db = this.getWritableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
String userId = cursor.getString(0);
Log.d("DB_USER_ID", userId);
return userId;
}
return null;
}
}
Model & Utility Classes
UserList.java
Java
package com.app.databaseloginregister.Item;
import java.io.Serializable;
public class UserList implements Serializable {
private String name, email, phone, password;
public UserList() {}
public UserList(String name, String email, String phone, String password) {
this.name = name;
this.email = email;
this.phone = phone;
this.password = password;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}
Method.java
Java
package com.app.databaseloginregister.Util;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import com.app.databaseloginregister.R;
public class Method {
private final Activity activity;
public SharedPreferences pref;
public SharedPreferences.Editor editor;
private final String myPreference = "login";
public final String pref_login = "pref_login";
public final String profileId = "profileId";
public Method(Activity activity) {
this.activity = activity;
pref = activity.getSharedPreferences(myPreference, 0);
editor = pref.edit();
}
/** Display a non-cancelable alert dialog */
public void alertBox(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setCancelable(false);
builder.setMessage(message);
builder.setPositiveButton(activity.getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// dismiss
}
});
builder.create().show();
}
}
Comments
Post a Comment