How to Create a User Registration Activity in Android using Firebase Authentication
In this tutorial, we will create a simple user registration screen using Firebase Authentication in an Android app. We will use Java for the logic and XML for the layout.
Step 1: Set Up Firebase in Your Android Project
- Add Firebase to your Android project: Go to the Firebase Console and add your Android app. Download the
google-services.json
file and place it in theapp
directory of your project. - Add Firebase SDK to your project: Open your
build.gradle
files and add the necessary dependencies.
// Project-level build.gradle
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.8' // Check for the latest version
}
}
// App-level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-auth:21.0.1' // Check for the latest version
}
Step 2: Create the Layout XML
Create a new XML layout file activity_register.xml
in the res/layout
directory. This file will define the UI components for the registration screen.
<?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:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:marginBottom="16dp"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:marginBottom="16dp"/>
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
</LinearLayout>
Step 3: Create the Java Activity
Create a new Java class RegisterActivity.java
in the com.apdim3.firebaseeverythink
package.
package com.apdim3.firebaseeverythink;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
public class RegisterActivity extends AppCompatActivity {
private EditText edEmail, edPassword;
private Button btnRegister;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
edEmail = findViewById(R.id.etEmail);
edPassword = findViewById(R.id.etPassword);
btnRegister = findViewById(R.id.btnRegister);
auth = FirebaseAuth.getInstance();
btnRegister.setOnClickListener(v -> {
String email = edEmail.getText().toString();
String password = edPassword.getText().toString();
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "Please fill the Information", Toast.LENGTH_SHORT).show();
} else if (password.length() < 6) {
Toast.makeText(this, "Password too weak", Toast.LENGTH_SHORT).show();
} else {
registerUser(email, password);
}
});
}
private void registerUser(String email, String password) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Registration successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "Registration failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Step 4: Add Activity to the Manifest
Make sure to declare your RegisterActivity
in the AndroidManifest.xml
.
<activity android:name=".RegisterActivity"></activity>
Step 5: Run Your Application
Ensure you have a connected device or emulator. Run your application, and you should see the registration screen. Enter an email and password to register a new user.
By following these steps, you've created a user registration screen in your Android app using Firebase Authentication. You can now expand on this foundation by adding additional features such as email verification, password reset, or a login screen.
Post a Comment