Java Android App Development Course, Using Authentication Features

In Android application development, the authentication feature is very important for protecting user information and data security. In this course, we will cover how to implement the authentication feature. We will explain step by step from basic concepts to actual code.

1. Understanding Authentication Feature

The authentication feature is the process of verifying who the user is. It is generally implemented through a username and password. However, nowadays various methods such as social login, biometrics, and two-factor authentication are being used. In Java Android apps, it can be implemented more easily through external libraries such as Firebase Authentication.

2. Setting Up the Project

Open Android Studio and create a new project. Follow the steps below:

  1. Open Android Studio and click “New Project”.
  2. Select Empty Activity and click Next.
  3. Enter the project name and package name. For example: com.example.authenticationdemo.
  4. Select Java as the language and click Finish.

3. Setting Up Firebase

Firebase is Google’s cloud service that makes it easy to implement authentication features. To set up a Firebase project, follow these steps:

  1. Log in to the Firebase console (https://console.firebase.google.com/).
  2. Add a new project.
  3. In the project settings, click the Android icon to register the Android app.
  4. Enter the package name and click “Add App”.
  5. Download the google-services.json file and add it to the app folder.
  6. Add the Google services plugin to your project’s build.gradle file.
  7. buildscript {
        dependencies {
            classpath 'com.google.gms:google-services:4.3.10' // Make sure to update to the latest version
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
            
  8. Add the Firebase Authentication library to the app’s build.gradle file.
  9. apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    
    dependencies {
        implementation 'com.google.firebase:firebase-auth:21.0.6' // Make sure to update to the latest version
    }
            

4. Designing the Layout

To implement the authentication feature, you need to design the user interface (UI). Modify the res/layout/activity_main.xml file to create a UI like the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Email"/>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Log In"/>

    <Button
        android:id="@+id/buttonSignUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sign Up"/>
    
</LinearLayout>
    

5. Implementing the Authentication Logic

Now, let’s implement the actual authentication logic. Open the MainActivity.java file and add the following code.

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button buttonSignIn;
    private Button buttonSignUp;

    private FirebaseAuth mAuth;

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

        editTextEmail = findViewById(R.id.editTextEmail);
        editTextPassword = findViewById(R.id.editTextPassword);
        buttonSignIn = findViewById(R.id.buttonSignIn);
        buttonSignUp = findViewById(R.id.buttonSignUp);

        mAuth = FirebaseAuth.getInstance();

        buttonSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn(editTextEmail.getText().toString(), editTextPassword.getText().toString());
            }
        });

        buttonSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createAccount(editTextEmail.getText().toString(), editTextPassword.getText().toString());
            }
        });
    }

    private void signIn(String email, String password) {
        mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, task -> {
                if (task.isSuccessful()) {
                    FirebaseUser user = mAuth.getCurrentUser();
                    Toast.makeText(MainActivity.this, "Login successful: " + user.getEmail(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Login failed.", Toast.LENGTH_SHORT).show();
                }
            });
    }

    private void createAccount(String email, String password) {
        mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, task -> {
                if (task.isSuccessful()) {
                    FirebaseUser user = mAuth.getCurrentUser();
                    Toast.makeText(MainActivity.this, "Registration successful: " + user.getEmail(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Registration failed.", Toast.LENGTH_SHORT).show();
                }
            });
    }
}
    

6. Running and Testing the App

After adding all the code, run the app to test if the functionality works well. Check if both login and sign-up functions work properly. It is also important to provide appropriate feedback to users and handle errors when they occur.

7. Implementing Additional Features

After building the basic sign-up and login functions, you can consider additional features like:

  • Email Verification: Set up email verification after registration to enhance security.
  • Password Reset: Add a feature that allows users to reset their password if they forget it.
  • Social Login: Implement authentication using social accounts like Google and Facebook.
  • User Profile Management: Add functionality to modify and view user profiles after authentication.

8. Conclusion

In this course, we learned how to implement authentication features in Android apps using Java. We were able to easily apply email/password-based login and registration functions through the Firebase Authentication library. Consider using various authentication methods to enhance the security of your application.

9. References