Firebase is a mobile(Android or iOS) and web application development platform that provides a suite of cloud-based tools and services offered by
In this tutorial, we'll build a simple Java Android app with Firebase for authentication that allows users to create accounts. sign in and sign out of the accounts. We'll also cover how to use your Android phone as an emulator.
Before starting this tutorial, you should know the basics of Android app development.
Additionally, you must have the following software installed on your computer:
To do this, you can connect a physical Android device or start an emulator.
To use an emulator, follow these steps:
To connect to a physical Android device, follow the following steps:
On your Android device( phone or tablet), open the settings and navigate to `About phone`. Click on 'Software Information'.
N.B.: There are other ways to connect your physical device to your Android studio as an emulator, such as wireless debugging, but in this tutorial, we'll use USB debugging.
In this step, we'll create the user interface and design. Navigate to the `app/res` folder; this is where most of the user interface changes will take place.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="slateblue"> #6F58C9</color>
<color name="black">#FF000000</color>
<color name="white"> #FFFFFFFF</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/slateblue"/>
<corners
android:radius="30dp"/>
</shape>
Repeat the steps excluding making it a launcher activity, this time for 'LoginActivity'.
Open the 'app/manifests/AndroidManifest.xml' file and remove the intent filter from the Main Activity block, also change the 'android:exported="true"' to 'android:exported="false"'. It should look like this:
<activity
android:name=".LoginActivity"
android:exported="false" />
<activity
android:name=".SignUpActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="false">
</activity>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@color/slateblue"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SignUpActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="24dp"
android:background="@drawable/custom_edittext">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textSize="36sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/slateblue"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_email"
android:background="@drawable/custom_edittext"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Email"
android:drawableLeft="@drawable/baseline_person_24"
android:drawablePadding="8dp"
android:textColor="@color/black" />
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/signup_password"
android:background="@drawable/custom_edittext"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:padding="8dp"
android:hint="Password"
android:drawableLeft="@drawable/baseline_lock_24"
android:drawablePadding="8dp"
android:textColor="@color/black"
/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Sign Up"
android:id="@+id/signup_button"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/slateblue"
app:cornerRadius = "20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loginRedirectText"
android:text="Already a user. Login"
android:layout_gravity="center"
android:padding="8dp"
android:layout_marginTop="10dp"
android:textColor="@color/slateblue"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".LoginActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="24dp"
android:background="@drawable/custom_edittext">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="36sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/slateblue"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/login_email"
android:background="@drawable/custom_edittext"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:padding="8dp"
android:hint="Email"
android:drawableLeft="@drawable/baseline_person_24"
android:drawablePadding="8dp"
android:textColor="@color/black" />
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/login_password"
android:background="@drawable/custom_edittext"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:padding="8dp"
android:hint="Password"
android:drawableLeft="@drawable/baseline_lock_24"
android:drawablePadding="8dp"
android:textColor="@color/black" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Login"
android:id="@+id/login_button"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/slateblue"
app:cornerRadius = "20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/signUpRedirectText"
android:text="Not yet registered? Sign Up"
android:layout_gravity="center"
android:padding="8dp"
android:layout_marginTop="10dp"
android:textColor="@color/slateblue"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Note: If you have more than one app on the project, go to the project overview, then click on 'app' and select which app you want to authenticate.
package com.example.firebaseauthapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class SignUpActivity extends AppCompatActivity {
// Declare any other necessary variables.
private FirebaseAuth auth;
private EditText signupEmail, signupPassword;
private Button signupButton;
private TextView loginRedirectText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//Initialize the FirebaseAuth instance in the onCreate()
auth = FirebaseAuth.getInstance();
signupEmail = findViewById(R.id.signup_email);
signupPassword = findViewById(R.id.signup_password);
signupButton = findViewById(R.id.signup_button);
loginRedirectText = findViewById(R.id.loginRedirectText);
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = signupEmail.getText().toString().trim();
String pass = signupPassword.getText().toString().trim();
if (user.isEmpty()){
signupEmail.setError("Email cannot be empty");
}
if(pass.isEmpty()){
signupPassword.setError("Password cannot be empty");
} else{
auth.createUserWithEmailAndPassword(user, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Signup Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
} else{
Toast.makeText(SignUpActivity.this, "Signup Failed" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
loginRedirectText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
}
});
}
}
package com.example.firebaseauthapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivity extends AppCompatActivity {
private FirebaseAuth auth;
private EditText loginEmail, loginPassword;
private TextView signupRedirectText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
loginEmail = findViewById(R.id.login_email);
loginPassword = findViewById(R.id.login_password);
loginButton = findViewById(R.id.login_button);
signupRedirectText = findViewById(R.id.signupRedirectText);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = loginEmail.getText().toString();
String pass = loginPassword.getText().toString();
if(!email.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
if (!pass.isEmpty()) {
auth.signInWithEmailAndPassword(email, pass)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class ));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginActivity.this, "Login Failed", Toast.LENGTH_SHORT).show();
}
});
} else {
loginPassword.setError("Password cannot be empty");
}
} else if (email.isEmpty()) {
loginEmail.setError("Email cannot be empty");
} else {
loginEmail.setError("Please enter a valid email");
}
}
});
signupRedirectText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
}
});
}
}
By following these steps, you have successfully integrated Firebase and set up user authentication for your application.
In this tutorial, we used Firebase to add user authentication to our Android Java application. We developed an application where users can create accounts, sign up, and sign out. We also covered how to use an Android phone as an emulator.
To expand your knowledge of Firebase in Android app development, refer to the Android and Firebase documentation.