Tutorial 2 - Register Page

 



This is Sample Project Which you can improve UI design & Coding skills .This is Register form with Firebase.

4) First go to "tools-Firebase" , create new DB and connect to android studio.(Authentication, Realtime Database )
5)Go to your Firebase account. Click Authentication and go to "sign in method" and enable "email/password".

You need to Download and paste these files to your "res" folder.  

If you get any trouble. Please Contact me on WhatsApp!!

activity_register

RegisterActivity

<?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"
tools:context=".RegisterActivity"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_gradient" />

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Covid Tracker"
android:textStyle="bold"
android:layout_marginTop="-40dp"
android:height="30dp"
android:fontFamily="sans-serif-smallcaps"
android:textAlignment="center"
android:textSize="25dp"
android:textColor="@color/white"
android:gravity="center_horizontal" />

<ScrollView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottie"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:layout_marginTop="20dp"
android:elevation="2dp"
app:lottie_repeatMode="restart"
app:lottie_repeatCount="1000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
app:lottie_autoPlay="true"
app:lottie_rawRes="@raw/register" />

<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To Register"
android:textAlignment="center"
android:fontFamily="@font/baloo"
android:textSize="35dp"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_gravity="center"/>


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@string/first_slide_desc"
android:gravity="center_horizontal" />

<EditText
android:id="@+id/fullName"
android:layout_width="350dp"
android:layout_height="45dp"
android:background="@drawable/input_style"
android:hint="Full Name"
android:drawableRight="@drawable/ic_user"
android:paddingRight="15dp"
android:layout_gravity="center"
android:textColor="#0d6e6a"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="30dp"
android:paddingLeft="20dp"
/>


<EditText
android:id="@+id/email"
android:layout_width="350dp"
android:layout_height="45dp"
android:background="@drawable/input_style"
android:hint="Email"
android:drawableRight="@drawable/ic_email"
android:paddingRight="15dp"
android:inputType="textEmailAddress"
android:layout_gravity="center"
android:textColor="#0d6e6a"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="10dp"
android:paddingLeft="20dp"
/>

<EditText
android:id="@+id/phone"
android:layout_width="350dp"
android:layout_height="45dp"
android:background="@drawable/input_style"
android:hint="Phone"
android:drawableRight="@drawable/ic_phone"
android:layout_gravity="center"
android:textColor="#0d6e6a"
android:paddingRight="15dp"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="10dp"
android:paddingLeft="20dp"
android:inputType="phone"
/>



<EditText
android:id="@+id/password"
android:layout_width="350dp"
android:layout_height="45dp"
android:background="@drawable/input_style"
android:hint="Password"
android:drawableRight="@drawable/ic_password"
android:layout_gravity="center"
android:textColor="#0d6e6a"
android:paddingRight="15dp"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="10dp"
android:paddingLeft="20dp"
android:inputType="textPassword"
/>



<Button
android:id="@+id/registerBtn"
android:layout_width="350dp"
android:layout_height="50dp"
android:background="@drawable/button_style"
android:text="Register"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#FFF"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>

<TextView
android:id="@+id/do_not_have_acc_btn"
android:layout_width="match_parent"
android:layout_height="22dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:onClick="Registertext"
android:text="Do you have an account? Login."
android:textColor="#F78D46"
android:textStyle="bold"
/>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:visibility="invisible"

/>

</LinearLayout>

</ScrollView>

</LinearLayout>

package com.rrmchathura.myfirstapp;

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.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.annotations.NotNull;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends AppCompatActivity {

public static final String TAG = "TAG";
private EditText mFullName,mEmail,mPassword,mPhone;
private Button mRegisterBtn;
ProgressBar progressBar;

private FirebaseAuth fAuth;
FirebaseFirestore fStore;

String userID;

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

mFullName = findViewById(R.id.fullName);
mEmail = findViewById(R.id.email);
mPassword = findViewById(R.id.password);
mPhone = findViewById(R.id.phone);
mRegisterBtn = findViewById(R.id.registerBtn);

fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
progressBar = findViewById(R.id.progressBar);

if (fAuth.getCurrentUser() != null){
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
finish();
}

mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
String fullName = mFullName.getText().toString();
String phone = mPhone.getText().toString();


if (TextUtils.isEmpty(fullName)){
mFullName.setError("Full Name is Required");
return;
}
if (TextUtils.isEmpty(email)){
mEmail.setError("Email is Required");
return;
}
if (TextUtils.isEmpty(phone)){
mPhone.setError("Phone is Required ");
return;
}
if (phone.length() < 10){
mPhone.setError("Invalid Number");
}
if (TextUtils.isEmpty(password)){
mPassword.setError("Password is Required");
return;
}
if (password.length() < 6){
mPassword.setError("Password Must be >= 6 Characters");
}

progressBar.setVisibility(View.VISIBLE);

//register the user
fAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull @NotNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(RegisterActivity.
this,"User is Created",Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.
collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).
addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG,"onSuccess: user Profile is created for "+ userID);
}
});
startActivity(new Intent(RegisterActivity.
this,LoginActivity.class));

}else {
Toast.makeText(RegisterActivity.this,
"Error !" + task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);

}
}
});

}
});

}

public void Registertext(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
finish();
}

}


Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X