Android Development: Build a Simple Login Screen in Just a Few Steps.

In this blog, I will show you how to create a basic login activity in Android. You will learn how to take user input (username & password) and validate it before redirecting the user to another screen.


Step 1: Create a New Android Project
  • Open Android Studio and create a New Project.
  • Select Empty Activity and click Next.
  • Name your project and choose Java or Kotlin as the language.
Step 2: Create Two Activities
You need two activities:
  1. UserPassActivity– Where users enter their username and password and Login button.
  2. WelcomeActivity – Where the entered username is displayed after successful login.
activity_user_pass.xml
XML Code Highlighter

Android XML Layout Code


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".UserPassActivity">

    <EditText
        android:id="@+id/edtUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter UserName"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/edtPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textSize="25sp" />

</LinearLayout>
        
UserPassActivity.java


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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class UserPassActivity extends AppCompatActivity {

    EditText edtUserName,edtPassword;
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_pass);

        edtUserName = findViewById(R.id.edtUserName);
        edtPassword = findViewById(R.id.edtPassword);
        btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String uname;
                String pass;
                uname = edtUserName.getText().toString().trim();
                pass = edtPassword.getText().toString().trim();

                if(uname.equals("vipul") && pass.equals("12345")) {
                    Intent i = new Intent(UserPassActivity.this, WelcomeUserActivity.class);
                    i.putExtra("user", uname);
                    startActivity(i);
                }
                else
                {
                    Toast.makeText(UserPassActivity.this,"Invalid Username and Password",Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}

activity_welcome.xml
XML Code Highlighter

Android XML Layout Code


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WelcomeUserActivity">

    <TextView
        android:id="@+id/txtUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome User"
        android:textSize="25sp"/>

</LinearLayout>
        
WelcomeActivity.java


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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class WelcomeUserActivity extends AppCompatActivity {

    TextView txtUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_welcome_user);
        txtUser = findViewById(R.id.txtUser);
        String user;
        Intent i = getIntent();
        user = i.getStringExtra("user");
        txtUser.setText("Welcome" + user);
    }
}



Post a Comment

0 Comments