Beginner 30 min Java Full project

Login and Register with SQLite in Android Studio (Java)

The classic login and register app with SQLite: register a user, sign in, and see a welcome screen. One of the most popular first projects on the channel.

What you'll learn

  • Create a users table in SQLite
  • Register users and prevent duplicates
  • Check login details against the database
  • Open a new screen after login

Before you start

  • Android Studio (any recent version)
  • Basic Java: variables, methods and if/else
  • An emulator or an Android phone

How it fits together

  1. 1Register screen
  2. 2DatabaseHelper
  3. 3Users table
  4. 4Login screen
  5. 5Home screen

Build it step by step

1. Create the project

In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. To copy the code below unchanged, use the package name com.example.loginregistersqlite.

2. Add two more screens

Right-click your package and choose New → Activity → Empty Views Activity twice, to create RegisterActivity and HomeActivity. Android Studio adds both to the manifest for you.

3. Build the three layouts

  • Login: a title, a username box, a password box, a Login button and a "Register here" link.
  • Register: the same form with a Sign up button and a "Login here" link.
  • Home: a welcome message and a Log out button.

Give the password box android:inputType="textPassword" so it shows dots, not letters.

4. Create the database helper

Add a class DatabaseHelper that extends SQLiteOpenHelper. Its onCreate makes a Users table with a UNIQUE username, so the same name can't register twice.

5. Never store passwords as typed

When someone registers, insertData creates a random salt, mixes it with the password and stores only the SHA-256 hash. At login, checkLogin repeats the same mix and compares the results. Even if someone copies the database, they can't read the passwords.

6. Register a user

In RegisterActivity, read both boxes when Sign up is tapped. If either is empty, show a message. Otherwise call insertData: true means success and opens Home, false means the username is taken.

7. Log in

In MainActivity, call checkLogin. It returns the username when the details match and null when they don't. On success, open HomeActivity and pass the name with intent.putExtra("USERNAME", ...).

8. Log out

HomeActivity shows "Welcome name". Log out and the Back button both return to the login screen.

9. Run and test

  1. Register alex with any password. You land on Home.
  2. Log out, then log in with the same details.
  3. Try a wrong password: you'll see Wrong username or password.
  4. Try registering alex again: you'll see That username is already taken.

If something goes wrong

  • The app crashes on Sign up: check that RegisterActivity and HomeActivity are in AndroidManifest.xml.
  • "Already taken" never appears: the username column needs UNIQUE. If you changed the table, uninstall the app once so it's recreated.
  • See the stored users: open App Inspection → Database Inspector while the app runs. You'll see hashes, not passwords.

What's improved over the video

Usernames are unique, passwords are hashed with a salt, the password box hides what you type, Back logs you out cleanly, and the screens respect Android 15's edge-to-edge layout.

Required setup

These are additions to an existing project—not replacement project files.

Register the two new screens

Merge additions

Where: app/src/main/AndroidManifest.xml

Add both activity entries inside your existing application element.

app/src/main/AndroidManifest.xml
<application ...>
    <activity
        android:name=".HomeActivity"
        android:exported="false" />
    <activity
        android:name=".RegisterActivity"
        android:exported="false" />
    <!-- keep your existing MainActivity entry -->
</application>

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.loginregistersqlite. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/loginregistersqlite/MainActivity.java

The login screen. Checks the username and password, then opens Home.

MainActivity.java
package com.example.loginregistersqlite;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/** The login screen. Opens HomeActivity when the username and password match. */
public class MainActivity extends AppCompatActivity {
    EditText username, password;
    TextView signupText;
    Button loginButton;
    DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        username = findViewById(R.id.userNameText);
        password = findViewById(R.id.passwordText);
        loginButton = findViewById(R.id.loginButton);
        signupText = findViewById(R.id.textSignup);
        databaseHelper = new DatabaseHelper(this);

        signupText.setOnClickListener(v -> startActivity(new Intent(this, RegisterActivity.class)));

        loginButton.setOnClickListener(v -> {
            String user = username.getText().toString().trim();
            String pass = password.getText().toString();
            if (user.isEmpty() || pass.isEmpty()) {
                Toast.makeText(this, R.string.error_empty, Toast.LENGTH_SHORT).show();
                return;
            }
            String loggedInUserName = databaseHelper.checkLogin(user, pass);
            if (loggedInUserName != null) {
                Intent intent = new Intent(this, HomeActivity.class);
                intent.putExtra("USERNAME", loggedInUserName);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, R.string.error_login, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Full source code