Building an Android Login Screen with Java

Nikhil
Make Android
Published in
4 min readAug 9, 2023

In today’s mobile-dominated world, secure user authentication is a fundamental aspect of any application. Creating a user-friendly and secure login screen is essential to ensure a seamless user experience. In this tutorial, I’ll walk you through the process of creating a basic login screen in Android Studio using Java. Let’s dive in!

Prerequisites:

  • Android Studio installed on your system
  • Basic understanding of Android development concepts
  1. Designing the Login Screen:

The design of your login screen is crucial for user engagement. We’ll start by creating the layout XML for the login screen. Navigate to the res/layout folder in your project and open activity_main.xml. This is where we'll define the UI elements. Here’s the content of the activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">

<!-- Username EditText -->

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Username"
android:inputType="text" />

<!-- Password EditText -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Password"
android:inputType="textPassword" />

<!-- Login Button -->
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />

</LinearLayout>

In this XML layout, I’ve used EditText widgets for username and password input fields and a Button for the login action.

2. Adding Functionality:

Now, let’s add the Java code to handle the login process. In your project’s src directory, navigate to the package containing your activities (usually com.example.yourappname) and open MainActivity.java. Here’s the content of the MainActivity.java file.

package com.example.loginscreenexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText editTextUsername, editTextPassword;
private Button buttonLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize UI elements
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);

// Set a click listener for the login button
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Retrieve entered username and password
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();

// Implement authentication logic here
if (username.equals("Admin") && password.equals("123")) {
// Successful login
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
} else {
// Failed login
Toast.makeText(MainActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}

In this Java code, we handle the login button click event. The entered username and password are retrieved from the EditText fields. For simplicity, I’ve hardcoded the username and password(“Admin” for username and “123” for password). In a real application, you would connect to a server for authentication.

Real-World Scenario: Secure Authentication

While our example showcases a straightforward approach, real-world applications demand robust security and user protection. For secure authentication, applications typically integrate with authentication servers and databases. Here’s how the process looks:

  1. User Input: Users provide their credentials in the app.
  2. Client-Server Interaction: The app sends the credentials securely to the authentication server.
  3. Server Authentication: The server verifies the credentials against its database. It generates a token or session ID upon successful authentication.
  4. Token Handling: The app receives the token and stores it securely (often using shared preferences or encrypted storage).
  5. Authorized Access: The app sends the token with subsequent requests to access protected resources. The server validates the token before providing access.

Output:

Conclusion: Creating a login screen in Android Studio with Java involves designing the layout and implementing the authentication logic. This tutorial provided a basic example to get you started. As you advance in your Android development journey, you can enhance the security and user experience of your login screen by incorporating more sophisticated authentication mechanisms and UI design.

Remember, security is a paramount concern when handling user authentication. Always follow best practices to protect user data and ensure a seamless user experience.

Thanks for Reading!

Please comment with questions and suggestions!! If this blog is helpful for you then, Please hit the clap! Follow on Medium, Please Follow and subscribe to Make Android for prompt updates on Android platform-related blogs.

Thank you!

--

--