Login and Register App with SQLite in Android Studio (2024)
Build a complete login and registration app that stores users in a local SQLite database. Register a user, sign in, and land on a home screen: the foundation of most real apps.
What you'll learn
- Create a users table with SQLiteOpenHelper
- Register users and block duplicates
- Check a username and password at login
- Move between screens with Intents
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
- 1Register screen
- 2DatabaseHelper
- 3Users table
- 4Login screen
- 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
- Register
alexwith any password. You land on Home. - Log out, then log in with the same details.
- Try a wrong password: you'll see Wrong username or password.
- Try registering
alexagain: you'll see That username is already taken.
If something goes wrong
- The app crashes on Sign up: check that
RegisterActivityandHomeActivityare inAndroidManifest.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.
Files affected
Check what to add and what to replace before copying code.
- app/src/main/AndroidManifest.xmlMerge additions
- app/src/main/java/com/example/loginregistersqlite/MainActivity.javaReplace file
- app/src/main/res/layout/activity_home.xmlAdd these lines
- app/src/main/res/layout/activity_main.xmlReplace file
- app/src/main/res/layout/activity_register.xmlAdd these lines
- app/src/main/res/values/colors.xmlReplace file
- app/src/main/java/com/example/loginregistersqlite/DatabaseHelper.javaAdd these lines
- app/src/main/java/com/example/loginregistersqlite/HomeActivity.javaAdd these lines
- app/src/main/java/com/example/loginregistersqlite/RegisterActivity.javaAdd these lines
- app/src/main/java/com/example/loginregistersqlite/Screens.javaAdd these lines
- app/src/main/res/values/strings.xmlReplace file
- app/src/main/res/values/themes.xmlReplace file
Required setup
These are additions to an existing project—not replacement project files.
Register the two new screens
Merge additionsWhere: app/src/main/AndroidManifest.xml
Add both activity entries inside your existing application element.
<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.
app/src/main/java/com/example/loginregistersqlite/MainActivity.javaThe login screen. Checks the username and password, then opens Home.
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();
}
});
}
}


