//DatabaseHelper.java

package com.example.yourapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "UserDB.db";
    public static final String TABLE_NAME = "users";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "USERNAME";
    public static final String COL_3 = "PASSWORD";

    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, USERNAME TEXT, PASSWORD TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertUser(String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, username);
        contentValues.put(COL_3, password);
        long result = db.insert(TABLE_NAME, null, contentValues);
        return result != -1;
    }

    public boolean checkUser(String username, String password) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE USERNAME=? AND PASSWORD=?", new String[]{username, password});
        return cursor.getCount() > 0;
    }

    public boolean userExists(String username) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE USERNAME=?", new String[]{username});
        return cursor.getCount() > 0;
    }
}

//LoginActivity.java

package com.example.yourapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    EditText username, password;
    Button loginBtn, signupRedirectBtn;
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.etUsername);
        password = findViewById(R.id.etPassword);
        loginBtn = findViewById(R.id.btnLogin);
        signupRedirectBtn = findViewById(R.id.btnSignupRedirect);

        db = new DatabaseHelper(this);

        loginBtn.setOnClickListener(v -> {
            String user = username.getText().toString();
            String pass = password.getText().toString();

            if (db.checkUser(user, pass)) {
                Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            } else {
                Toast.makeText(LoginActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
            }
        });

        signupRedirectBtn.setOnClickListener(v -> startActivity(new Intent(LoginActivity.this, SignupActivity.class)));
    }
}

//SignupActivity

package com.example.yourapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class SignupActivity extends AppCompatActivity {

    EditText username, password, confirmPassword;
    Button signupBtn;
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        username = findViewById(R.id.etUsernameSignup);
        password = findViewById(R.id.etPasswordSignup);
        confirmPassword = findViewById(R.id.etConfirmPassword);
        signupBtn = findViewById(R.id.btnSignup);

        db = new DatabaseHelper(this);

        signupBtn.setOnClickListener(v -> {
            String user = username.getText().toString();
            String pass = password.getText().toString();
            String cpass = confirmPassword.getText().toString();

            if (user.equals("") || pass.equals("") || cpass.equals("")) {
                Toast.makeText(SignupActivity.this, "All fields are required", Toast.LENGTH_SHORT).show();
            } else if (!pass.equals(cpass)) {
                Toast.makeText(SignupActivity.this, "Passwords do not match", Toast.LENGTH_SHORT).show();
            } else if (db.userExists(user)) {
                Toast.makeText(SignupActivity.this, "User already exists", Toast.LENGTH_SHORT).show();
            } else {
                if (db.insertUser(user, pass)) {
                    Toast.makeText(SignupActivity.this, "Signup Successful", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(SignupActivity.this, LoginActivity.class));
                    finish();
                } else {
                    Toast.makeText(SignupActivity.this, "Signup Failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

//activity_login.xml

//activity_signup.xml

Steps to Integrate Login/Signup

🔹 1. Add Activities to AndroidManifest.xml
Open AndroidManifest.xml and add:

This makes LoginActivity the first screen when the app launches.

  1. Connect to Your Main App Screen Let’s say your main screen is MainActivity.java, already existing. In LoginActivity.java, modify this part:
if (db.checkUser(user, pass)) {
    Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.putExtra("username", user); // optional
    startActivity(intent);
    finish(); // prevent back navigation to login
}
  1. Access the Logged-in User in MainActivity (Optional) In MainActivity.java, you can get the logged-in username:
String username = getIntent().getStringExtra("username");
TextView welcome = findViewById(R.id.tvWelcome);
welcome.setText("Welcome, " + username + "!");
  1. (Optional) Add a Logout Button in MainActivity To go back to login screen:
Button logoutBtn = findViewById(R.id.btnLogout);
logoutBtn.setOnClickListener(v -> {
    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // clear back stack
    startActivity(intent);
});
  1. Add the DatabaseHelper.java to Your java Folder
    Just right-click your java package, choose New > Java Class, name it DatabaseHelper, and paste the provided code.

  2. Add XML Layouts
    Copy:
    activity_login.xml → res/layout/activity_login.xml
    activity_signup.xml → res/layout/activity_signup.xml
    Use the layout editor or just paste the XML directly.