Android Firebase Login Tutorial
In this tutorial, we will learn how to implement a login activity in an Android application using Firebase Authentication. Follow the steps below to create a simple login screen with email and password authentication.
Step 1: Setup Firebase in your Android Project
First, you need to set up Firebase in your Android project. Follow these steps:
- Create a new project on Firebase Console.
- Add your Android app to the Firebase project by providing the package name.
- Download the
google-services.json
file and place it in theapp
directory of your project. - Add Firebase SDK dependencies in your
build.gradle
files.
Step 2: Update build.gradle files
Project-level build.gradle
buildscript {
repositories {
// Check that you have the following line (if not, add it)
google()
mavenCentral()
}
dependencies {
// Add these dependencies
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
App-level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.apdim3.firebaseeverythink"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.google.firebase:firebase-auth:21.0.3'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
}
Step 3: Create the LoginActivity Layout
Create a new layout XML file named activity_login.xml
in the res/layout
directory with the following content:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".LoginActivity">
<EditText
android:id="@+id/editTextEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintMarginStart="32dp"
app:layout_constraintMarginEnd="32dp"
app:layout_constraintMarginTop="64dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/editTextEmail"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintMarginStart="32dp"
app:layout_constraintMarginEnd="32dp"
app:layout_constraintMarginTop="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintTop_toBottomOf="@id/editTextPassword"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintMarginStart="32dp"
app:layout_constraintMarginEnd="32dp"
app:layout_constraintMarginTop="32dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Create the LoginActivity Class
Create a new Java class named LoginActivity
in the com.apdim3.firebaseeverythink
package with the following content:
package com.apdim3.firebaseeverythink;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivity extends AppCompatActivity {
private EditText edEmail, edPassword;
private Button btnLogin;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
edEmail = findViewById(R.id.editTextEmail);
edPassword = findViewById(R.id.editTextPassword);
btnLogin = findViewById(R.id.buttonLogin);
auth = FirebaseAuth.getInstance();
btnLogin.setOnClickListener(v -> {
String sEmail = edEmail.getText().toString();
String sPass = edPassword.getText().toString();
userLogin(sEmail, sPass);
});
}
private void userLogin(String email, String password) {
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
// Redirect to another activity or update UI
} else {
// If sign in fails, display a message to the user
}
});
}
}
Conclusion
By following these steps, you have created a login screen in your Android application using Firebase Authentication. Now, users can sign in with their email and password. You can further enhance this example by adding form validation and better error handling.
Post a Comment