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.
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 alex with 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 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.
Files affected
Check what to add and what to replace before copying code.