Firebase Authentication 03 – Register form

Very firstly you need to connect your app to Firebase . After that you can use this article to create a Register form using Firebase Authentication. So let’s start “Firebase Authentication 03 – Register form” article. And also we can use firebase android to google analytics android, firebase push notification, firebase database, firebase analytics app like wise.

As in the Login form creating activity, we need a xml design for the register form also. So let’s design register form first.

I am going to design android UI like this.

Firebase Authentication 03 - Register form
Register form design | Firebase Authentication 03 – Register form

Create a new empty activity called “RegisterActivity”. In here, I used a drawable xml file for the background. You can see a Blue shape background. Others are 3 Editboxs and one button. I used two Textviews to display “sign up” title and “Already have an account? sign in”. That’s it. very simple. Let’s design background drawable resource xml file.


Drawable XML file

To create a drawable file, right click on the drawable file and click new. Then select “Drawable Resource File”. In here I name it as “background”. Here is the code for that xml file.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:bottomLeftRadius="100dp"
        android:bottomRightRadius="300dp"/>
    <solid android:color="#04095E"/>
</shape>

In here “<?xml version=”1.0″ encoding=”utf-8″?>” is the default line, when you create a new file. Using “shape” tag, you can create a shape in the drawable file. I used “corners” tag to adjust corners in the shape. you can see I added radius for the bottom left and right corners.


Let’s design activity_register.xml

Go to RegisterActivity’s layout resource xml file. Here is the code for that.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:orientation="vertical">

        <ProgressBar
            android:id="@+id/register_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:visibility="invisible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SIGN UP"
                android:layout_marginTop="150dp"
                android:textAlignment="center"
                android:textColor="#FFFFFF"
                android:layout_marginBottom="10dp"
                android:textSize="15dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/setup_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="30dp"
                android:background="#ffffff"
                android:padding="10dp"
                android:gravity="center"
                android:ems="10"
                android:inputType="textPersonName" />

            <EditText
                android:id="@+id/reg_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginLeft="50dp"
                android:gravity="center"
                android:background="#ffffff"
                android:padding="10dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="10dp"
                android:ems="10"
                android:inputType="textPersonName" />

            <EditText
                android:id="@+id/reg_confirm_pass"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Confirm Password"
                android:gravity="center"
                android:background="#ffffff"
                android:padding="10dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="10dp"
                android:ems="10"
                android:inputType="textPersonName" />

            <Button
                android:id="@+id/reg_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="50dp"
                android:layout_marginRight="50dp"
                android:background="#ffffff"
                android:textColor="#04095E"
                android:text="create account" />
            <TextView
                android:id="@+id/logTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Already have an Account? Sign In"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="30dp"
                android:textColor="#ffffff"
                android:textAlignment="center"/>
            
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

Let’s code RegisterActivity.java class

Open you RegisterActivity.java class. Here is the code for that.


package com.example.takemethereapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
import com.google.firebase.auth.FirebaseUser;

public class RegisterActivity extends AppCompatActivity {

    private EditText regEmail;
    private EditText regPass;
    private EditText regConfirmPass;
    private Button regBtn, reg_login_btn;
    private ProgressBar registerProgress;
    private TextView logText;

    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();

        regEmail = (EditText) findViewById(R.id.setup_name);
        regPass = (EditText) findViewById(R.id.reg_password);
        regConfirmPass = (EditText) findViewById(R.id.reg_confirm_pass);
        regBtn = (Button) findViewById(R.id.reg_btn);
        registerProgress = (ProgressBar) findViewById(R.id.register_progressbar);
        logText = findViewById(R.id.logTextView);


        logText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        regBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = regEmail.getText().toString();
                String pass = regPass.getText().toString();
                String conPass = regConfirmPass.getText().toString();

                if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(conPass)) {
                    registerProgress.setVisibility(View.VISIBLE);

                    if (pass.equals(conPass)) {
                        mAuth.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()){
                                    Intent goSetuo = new Intent(RegisterActivity.this, SetupActivity.class);
                                    startActivity(goSetuo);
                                    finish();
                                }else {
                                    String error = task.getException().getMessage();
                                    Toast.makeText(RegisterActivity.this,"invalid registration",Toast.LENGTH_LONG).show();

                                }
                                registerProgress.setVisibility(View.INVISIBLE);
                            }
                        });

                    } else {
                    Toast.makeText(RegisterActivity.this,"Passwords are not match",Toast.LENGTH_LONG).show();

                }
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser CurrentUser = mAuth.getCurrentUser();
        if (CurrentUser != null) {
            sendToMain();
        } else {
            // No user is signed in
        }
    }

    private void sendToMain() {
        Intent goMain = new Intent(RegisterActivity.this, MainActivity.class);
        startActivity(goMain);
        finish();
    }
}

  • onstart() method – This method will run when this LoginActivity.java class start. So it will check App user is a current user or not. It means the user already registered to system or not. If user is registered, app will redirect to MainActivity.class (sendToMain() is a function for create intent to redirect to MainActivity.class) click here to refer Android Intent.
  • Above oncreate() method, you have to define Firebase instance and other instance regarding to Edittexts, Textviews, Buttons and the progressbar (I used progree bar in here)

Now you can run your application. If you are not log into app, app will redirect you to log in page. (If you want to create a log in form click here to refer.) Then navigate to sign up page and give an email and a password with the confirm password. Then try to register a user.

After that go to Firebase console and select the Authentication tab. In the Users tab you can see the newly registered user. And try log in using that credentials.

Thank you

Firebase SDK- Click here to refer

Firebase Analytics Android – Click here to refer

Android crashlytics – Click here to refer

Make sure to leave a comment.

If you are a newbie to Android. Follow and refer articles. Click below link to get started.

Other articles from different categories.

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x