How to create a Registration Page Activity in Android using the Firebase ?
Hello Guys. In this blog, I am going to show how to create a registration activity in android using the firebase which must be enabled in your android project.
If you want to have a look at how our application will look and work, then check out my youtube video. The link is given below.
Watch my YouTube video to get more clarity on how to create a registration activity using the firebase.
In upcoming blogs, I will show how to create a login and reset password activity.
Here, we are using the "Firebase" in our android project to create a registration activity and a login activity because firebase provides us a central authentication mechanism where you can easily manage the users who registered in your android application.
Firebase provides us the methods using we can create users using email and password and store the detail on Google servers which makes our work lot easier as compared to the scenario where we have to take care of all the data and create an authentication mechanism. Also, Firebase provides us the methods to send the reset password link to the email.
In my previous blog, I have explained and showed all the steps in detail on how to add or integrate your android project to the firebase. Adding your android project to the firebase is one of the main prerequisites if you want to create a registration and login activity. If you have not integrated till now, check out my previous blog. The link is given below.
How to Add or Integrate Your Android Project to the Firebase ?
Step 1:-
Create an empty activity named "SignupActivity". This will create one java file "SignupActivity.java" and one XML layout file "activity_signup.xml".
Step 2:-
The following are the contents of the "activity_signup.xml" layout file. Copy and paste it.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".SignupActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:layout_width="@dimen/logo_w_h"
android:layout_height="@dimen/logo_w_h"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:src="@mipmap/ic_launcher" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/reg_fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_fullname"
android:inputType="textPersonName"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/reg_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/reg_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorAccent"
android:text="@string/action_sign_in_short"
android:textColor="@android:color/black"
android:textStyle="bold" />
<Button
android:id="@+id/reg_sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_link_to_login"
android:textAllCaps="false"
android:textColor="@color/colorAccent"
android:textSize="15dp" />
</LinearLayout>
<ProgressBar
android:id="@+id/reg_progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here, we are adding three text fields, one for providing the full name, one for providing the email id, and the last one for providing the password for registration. Then, we are adding one button which would perform our registration process and the second button which redirects to the login activity which I would create in my next blog and video. So, keep checking my blogs for the next part of this tutorial series.
Step 3:-
The following are the contents of the "SignupActivity.java" file. Copy and paste it.
package techy.vaibhav.demo_project;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
//import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
public class SignupActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword, inputName;
private Button btnSignIn, btnSignUp;
private ProgressBar progressBar;
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//Get Firebase auth instance
firebaseAuth = FirebaseAuth.getInstance();
btnSignIn = findViewById(R.id.reg_sign_in_button);
btnSignUp = findViewById(R.id.sign_up_button);
inputName = findViewById(R.id.reg_fullname);
inputEmail = findViewById(R.id.reg_email);
inputPassword = findViewById(R.id.reg_password);
progressBar = findViewById(R.id.reg_progressBar);
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Will get switch to Login Activity", Toast.LENGTH_SHORT).show();
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
final String fullname = inputName.getText().toString().trim();
if (TextUtils.isEmpty(fullname)) {
Toast.makeText(getApplicationContext(), "Enter Full Name!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "User has been Created or not:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
//startActivity(new Intent(SignupActivity.this, MainActivity.class));
firebaseUser = firebaseAuth.getCurrentUser();
UserProfileChangeRequest displayName = new UserProfileChangeRequest.Builder().setDisplayName(fullname).build();
//UserProfileChangeRequest phone_number = new UserProfileChangeRequest.Builde
firebaseUser.updateProfile(displayName).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.d("SignupActivity", "Profile updated");
}
}
});
Toast.makeText(getApplicationContext(),"Registration Completed Successfully", Toast.LENGTH_SHORT).show();
//gotoUserProfile();
finish();
}
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
Let's have a look at the code:-
private EditText inputEmail, inputPassword, inputName;
private Button btnSignIn, btnSignUp;
private ProgressBar progressBar;
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;All the variables are declared.
//Get Firebase auth instancefirebaseAuth = FirebaseAuth.getInstance();
btnSignIn = findViewById(R.id.reg_sign_in_button);
btnSignUp = findViewById(R.id.sign_up_button);
inputName = findViewById(R.id.reg_fullname);
inputEmail = findViewById(R.id.reg_email);
inputPassword = findViewById(R.id.reg_password);
progressBar = findViewById(R.id.reg_progressBar);
All the variables are initialized. Here firebaseAuth is the instance of firebase authentication.
Now, I will add action on two buttons namely btnSignIn and btnSignUp.
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Will get switch to Login Activity", Toast.LENGTH_SHORT).show();
finish();
}
});
Currently, on clicking the above button, a toast message will come but in upcoming blogs, we are going to add an action where the button will switch the view from Registration activity to Login Activity.
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
final String fullname = inputName.getText().toString().trim();
if (TextUtils.isEmpty(fullname)) {
Toast.makeText(getApplicationContext(), "Enter Full Name!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "User has been Created or not:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
//startActivity(new Intent(SignupActivity.this, MainActivity.class));
firebaseUser = firebaseAuth.getCurrentUser();
UserProfileChangeRequest displayName = new UserProfileChangeRequest.Builder().setDisplayName(fullname).build();
//UserProfileChangeRequest phone_number = new UserProfileChangeRequest.Builde
firebaseUser.updateProfile(displayName).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.d("SignupActivity", "Profile updated");
}
}
});
Toast.makeText(getApplicationContext(),"Registration Completed Successfully", Toast.LENGTH_SHORT).show();
//gotoUserProfile();
finish();
}
}
});
}
});
In the above part code, I am getting the input values from the user and validating the input values. For example, any text field should not be empty. Also, the length of the password must be greater than 6.
After validation, I am passing the value of the email and password to the method of Firebase
authentication namely createUserWithEmailAndPassword to create the user with email id and password provided if everything is correct.
Now the Registration Activity is all set to run. As the Registration part is not our main activity, the login activity is our main activity which should initiate on running the application but we have not created the Login Activity till now. So after running the application, directly our Registration activity should start as we have to check whether our code is working or not.
To directly start the Registration activity as the main activity, Go to the AndroidManifest.xml file.
Change the following code:-
From:-
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To
<activity android:name=".SignupActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now run the application. Input all the required values like Full name, email id, and password and click on "Register". Registration should be completed successfully.
In the next part of the blog, I am going to create the login activity and will integrate it into the same application. So stay tuned.
For More Posts, Follow my blog.
You can follow me on Instagram:-
http://instagram.com/vaibhavu3u
Comments
Post a Comment