Beginner 15 min Java Full project

Simple Login App in Android Studio (Java)

Your first login screen: check a username and password and show a welcome message or an error. The perfect first Android app.

What you'll learn

  • Design a login layout
  • Read what the user typed
  • Compare with the correct details
  • Show a Toast for success or failure

Before you start

  • Android Studio (any recent version)
  • An emulator or an Android phone

How it fits together

  1. 1Login form
  2. 2Check username and password
  3. 3Show error under the field
  4. 4NewActivity welcome

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.simplelogin.

2. Add the welcome screen

Right-click your package and choose New → Activity → Empty Views Activity. Name it NewActivity.

3. Build the form with Material text fields

In activity_main.xml, wrap each TextInputEditText in a TextInputLayout. You get a floating label, a place for error messages, and for the password, app:endIconMode="password_toggle" adds the eye icon that shows or hides it.

4. Hide the password

Give the password field android:inputType="textPassword", so it shows dots.

5. Check the details

When Log in is tapped, read both fields. If one is empty, call setError on its TextInputLayout so the message appears right under that box.

6. Compare with the right values

This first app compares with fixed values: username user, password 123456. If they match, open NewActivity with an Intent and pass the username along. If not, show Wrong username or password.

7. Welcome the user

NewActivity reads the username from the intent and shows "Welcome, user!". Log out calls finish(), which returns to the login screen.

8. Run it

Try wrong details first to see the error, then log in with user / 123456.

Next steps

Real apps never keep passwords in code. To store real accounts, follow Login and Register with SQLite or Firebase Authentication.

What's improved over the video

The welcome screen is included (the original post left it out), errors show under the right field, the password hides with a show/hide toggle, and the layout uses Material 3.

Application code

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

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

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

Checks the username and password and opens the welcome screen.

MainActivity.java
package com.example.simplelogin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

/**
 * A first login screen: checks the username and password against fixed values.
 * Real apps check against a database or a server; see the SQLite and Firebase login tutorials.
 */
public class MainActivity extends AppCompatActivity {
    private static final String USERNAME = "user";
    private static final String PASSWORD = "123456";

    TextInputLayout loginLayout, passwordLayout;
    TextInputEditText loginText, passwordText;
    Button btnLogin;

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

        loginLayout = findViewById(R.id.loginLayout);
        passwordLayout = findViewById(R.id.passwordLayout);
        loginText = findViewById(R.id.login);
        passwordText = findViewById(R.id.password);
        btnLogin = findViewById(R.id.buttonLogin);

        btnLogin.setOnClickListener(view -> {
            String username = String.valueOf(loginText.getText()).trim();
            String password = String.valueOf(passwordText.getText());

            // Show problems right under the box they belong to.
            loginLayout.setError(username.isEmpty() ? getString(R.string.error_username) : null);
            passwordLayout.setError(password.isEmpty() ? getString(R.string.error_password) : null);
            if (username.isEmpty() || password.isEmpty()) return;

            if (username.equals(USERNAME) && password.equals(PASSWORD)) {
                Intent intent = new Intent(this, NewActivity.class);
                intent.putExtra("USERNAME", username);
                startActivity(intent);
                passwordText.setText("");
            } else {
                passwordLayout.setError(getString(R.string.error_invalid));
            }
        });
    }
}

Full source code