when user enter input in signup from then on the firebase database the data misplaced

Viewed 15

I have created a signup form for users to signup. When the user fills in the details on the Firebase server, the details did not show in the exact place.

I have attached all the required code and screenshots.

This XML code I am using.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dark"
    android:gravity="center"
    android:paddingLeft="16sp"
    android:paddingRight="16sp"
    android:paddingBottom="5sp"
    tools:context=".Register">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="190sp"
        android:contentDescription="@string/todo"
        android:src="@drawable/logo" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="190sp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/Name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="5sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/user_18"
                android:drawablePadding="10sp"
                android:ems="10"
                android:hint="@string/name"
                android:inputType="text"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <EditText
                android:id="@+id/EmailId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="65sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/envelope_18"
                android:drawablePadding="10sp"
                android:ems="10"
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <EditText
                android:id="@+id/Password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="125sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/lock_18"
                android:drawablePadding="10sp"
                android:ems="10"
                android:hint="@string/pass"
                android:inputType="textPassword"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <EditText
                android:id="@+id/Re_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="185sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/lock_18"
                android:drawablePadding="10sp"
                android:ems="10"
                android:hint="@string/rpass"
                android:inputType="textPassword"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <EditText
                android:id="@+id/Mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="245sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/phone_call_18"
                android:drawablePadding="10sp"
                android:ems="10"
                android:hint="@string/mobile_number"
                android:inputType="number"
                android:padding="14sp"
                android:textColorHint="#616161" />


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

                <EditText
                    android:id="@+id/DOB"
                    android:layout_width="170sp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="305sp"
                    android:background="@drawable/boder"
                    android:drawableStart="@drawable/calendar_18"
                    android:drawablePadding="10sp"
                    android:focusable="false"
                    android:hint="@string/date_of_birth"
                    android:importantForAutofill="no"
                    android:inputType="date"
                    android:padding="14sp"
                    android:textColorHint="#616161" />

                <EditText
                    android:id="@+id/Gender"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30sp"
                    android:layout_marginTop="305sp"
                    android:background="@drawable/boder"
                    android:drawableStart="@drawable/male_18"
                    android:drawablePadding="10sp"
                    android:ems="10"
                    android:hint="@string/gender"
                    android:importantForAutofill="no"
                    android:inputType="text"
                    android:padding="14sp"
                    android:textColorHint="#616161" />


            </LinearLayout>


            <EditText
                android:id="@+id/School"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="365sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/school_18"
                android:drawablePadding="10sp"
                android:hint="@string/school_name"
                android:inputType="text"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <EditText
                android:id="@+id/Classes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="425sp"
                android:autofillHints=""
                android:background="@drawable/boder"
                android:drawableStart="@drawable/graduation_cap_18"
                android:drawablePadding="10sp"
                android:hint="@string/clas"
                android:inputType="text"
                android:padding="14sp"
                android:textColorHint="#616161" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/State"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="490sp"
                    android:layout_marginEnd="20sp"
                    android:background="@drawable/boder"
                    android:drawableStart="@drawable/marker_18"
                    android:drawablePadding="10sp"
                    android:hint="@string/select_state"
                    android:importantForAutofill="no"
                    android:inputType="text"
                    android:padding="14sp"
                    android:textColorHint="#616161" />

                <EditText
                    android:id="@+id/City"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="490sp"
                    android:background="@drawable/boder"
                    android:drawableStart="@drawable/home_18"
                    android:drawablePadding="10sp"
                    android:ems="10"
                    android:hint="@string/city"
                    android:importantForAutofill="no"
                    android:inputType="text"
                    android:padding="14sp"
                    android:textColorHint="#616161" />


            </LinearLayout>

            <Button
                android:id="@+id/sign"
                android:layout_width="240sp"
                android:layout_height="wrap_content"
                android:layout_marginStart="50sp"
                android:layout_marginTop="550sp"
                android:background="@drawable/btnshape"
                android:text="@string/sign_up"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="24sp" />

        </RelativeLayout>


    </ScrollView>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:elevation="15sp"
        android:visibility="gone" />

</RelativeLayout>

Java Code***********************************************************************************************

package com.example.studysagar;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.FirebaseAuthWeakPasswordException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Register extends AppCompatActivity {

    private static final String TAG = "Register";
    private EditText Name, EmailId, Password, RePassword, Mobile, DoB, Gender, School, Classes, State, City;
    private Button sign;
    private DatePickerDialog picker;
    private ProgressDialog loadingBar;

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

        getSupportActionBar().setTitle("Registration");

        Toast.makeText(Register.this, "You can Registered now", Toast.LENGTH_SHORT).show();

        loadingBar = new ProgressDialog(this);

     /*  // open login activity
        Button SignIn = findViewById(R.id.SignIn);
        SignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, signin.class);
                startActivity(intent);
            }
        });*/

        Name = findViewById(R.id.Name);
        EmailId = findViewById(R.id.EmailId);
        Password = findViewById(R.id.Password);
        RePassword = findViewById(R.id.Re_password);
        Mobile = findViewById(R.id.Mobile);
        DoB = findViewById(R.id.DOB);
        Gender = findViewById(R.id.Gender);
        School = findViewById(R.id.School);
        Classes = findViewById(R.id.Classes);
        State = findViewById(R.id.State);
        City = findViewById(R.id.City);
        sign = findViewById(R.id.sign);


        // Setting of date picker
        DoB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar calendar = Calendar.getInstance();
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);

                // Date Picker dialogue
                picker = new DatePickerDialog(Register.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        DoB.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                    }
                }, year, month, day);
                picker.show();
            }
        });


        sign.setOnClickListener(view -> {


            // Obtain entered data
            String name = Name.getText().toString();
            String emailid = EmailId.getText().toString();
            String password = Password.getText().toString();
            String Repass = RePassword.getText().toString();
            String mobile = Mobile.getText().toString();
            String dateOfBirth = DoB.getText().toString();
            String gender = Gender.getText().toString();
            String school = School.getText().toString();
            String Clas = Classes.getText().toString();
            String state = State.getText().toString();
            String city = City.getText().toString();


            //Validate Mobile Number using Matcher and Patter (Regular Expression)
            String mobileRegex = "[6-9][0-9]{9}"; // first  number can be{6,7,8,9}
            Matcher mobileMatcher;
            Pattern mobilePattern = Pattern.compile(mobileRegex);
            mobileMatcher = mobilePattern.matcher(mobile);

            //Condition

            if (TextUtils.isEmpty(name)) {
                Toast.makeText(Register.this, "Please enter your name!", Toast.LENGTH_LONG).show();
                Name.setError("Full name is required");
                Name.requestFocus();
                return;
            } else if (TextUtils.isEmpty(emailid)) {
                Toast.makeText(Register.this, "Please enter your email!", Toast.LENGTH_LONG).show();
                EmailId.setError("email is required");
                EmailId.requestFocus();
                return;
            } else if (!Patterns.EMAIL_ADDRESS.matcher(emailid).matches()) {
                Toast.makeText(Register.this, "Please re-enter your email!", Toast.LENGTH_LONG).show();
                EmailId.setError("Valid email is required");
                EmailId.requestFocus();
                return;

            } else if (TextUtils.isEmpty(password)) {
                Toast.makeText(Register.this, "Please enter your password!", Toast.LENGTH_LONG).show();
                Password.setError("password is required");
                Password.requestFocus();
            } else if (Password.length() < 8) {
                Toast.makeText(Register.this, "Password should be at least 8 digit", Toast.LENGTH_LONG).show();
                Password.setError("password is too weak");
                Password.requestFocus();
            } else if (TextUtils.isEmpty(Repass)) {
                Toast.makeText(Register.this, "Please enter your password again", Toast.LENGTH_LONG).show();
                RePassword.setError("password is required");
                RePassword.requestFocus();
            } else if (!password.equals(Repass)) {
                Toast.makeText(Register.this, "Please enter same password", Toast.LENGTH_LONG).show();
                RePassword.setError("password not match");
                RePassword.requestFocus();
                // Clear the entered password
                Password.clearComposingText();
                RePassword.clearComposingText();
            } else if (TextUtils.isEmpty(mobile)) {
                Toast.makeText(Register.this, "Please enter your mobile number", Toast.LENGTH_LONG).show();
                Mobile.setError("mobile number is required");
                Mobile.requestFocus();
            } else if (Mobile.length() != 10) {
                Toast.makeText(Register.this, "Please enter your mobile number", Toast.LENGTH_LONG).show();
                Mobile.setError("mobile number should be 10 digits");
                Mobile.requestFocus();
            } else if (!mobileMatcher.find()) {
                Toast.makeText(Register.this, "Please enter your mobile number", Toast.LENGTH_LONG).show();
                Mobile.setError("mobile number is not valid");
                Mobile.requestFocus();

            } else if (TextUtils.isEmpty(dateOfBirth)) {
                Toast.makeText(Register.this, "Please enter your date of birth", Toast.LENGTH_LONG).show();
                DoB.setError("date of birth is required");
                DoB.requestFocus();
                return;
            } else if (TextUtils.isEmpty(gender)) {
                Toast.makeText(Register.this, "Please enter your gender", Toast.LENGTH_LONG).show();
                Gender.setError("Gender is required");
                Gender.requestFocus();
                return;
            } else if (TextUtils.isEmpty(school)) {
                Toast.makeText(Register.this, "Please enter your school name", Toast.LENGTH_LONG).show();
                School.setError("school name is required");
                School.requestFocus();
                return;
            } else if (TextUtils.isEmpty(Clas)) {
                Toast.makeText(Register.this, "Please enter your Class", Toast.LENGTH_LONG).show();
                Classes.setError("Class is required");
                Classes.requestFocus();
                return;
            } else if (TextUtils.isEmpty(state)) {
                Toast.makeText(Register.this, "Please enter State", Toast.LENGTH_LONG).show();
                State.setError("State is required");
                State.requestFocus();
                return;
            } else if (TextUtils.isEmpty(city)) {
                Toast.makeText(Register.this, "Please enter your city name", Toast.LENGTH_LONG).show();
                City.setError("city name is required");
                City.requestFocus();
                return;
            } else {

                loadingBar.setTitle("Registering");
                loadingBar.setMessage("Please Wait, while we are register");
                loadingBar.show();

                registerUser(name, emailid, password, Repass, dateOfBirth, school, Clas, state, city, gender, mobile);
            }

        });

    }


    // Register User using the credentials given
    private void registerUser(String name, String emailid, String password, String repass, String mobile, String dateOfBirth,
                              String gender, String school, String Clas, String state, String city) {
        FirebaseAuth auth = FirebaseAuth.getInstance();

        // Create User Profile.
        auth.createUserWithEmailAndPassword(emailid, password).addOnCompleteListener(Register.this,
                new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            FirebaseUser firebaseUser = auth.getCurrentUser();

                            // Enter user data into0 the firebase Realtime Database.
                            ReadWriteUserDetails writeUserDetails = new ReadWriteUserDetails(name, emailid, password, mobile,
                                    dateOfBirth, gender, school, Clas, state, city);

                            //Update Display Name of the user
                            UserProfileChangeRequest profileChangeRequest =
                                    new UserProfileChangeRequest.Builder().setDisplayName(name).build();
                            firebaseUser.updateProfile(profileChangeRequest);


                            // Extracting User reference from Database for "Registered User"
                            DatabaseReference referenceProfile = FirebaseDatabase.getInstance().getReference("Registered Student");

                            referenceProfile.child(firebaseUser.getUid()).setValue(writeUserDetails).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {

                                        // Send verification email.
                                        firebaseUser.sendEmailVerification();

                                        Toast.makeText(Register.this, "Registered Successfully", Toast.LENGTH_SHORT).show();

                                        loadingBar.dismiss();

                                      /*  // Open user referenceProfile after successful register
                                        Intent intent = new Intent(Register.this, StudentProfile.class);
                                        // To prevent Use from returning back to Register Activity on pressing back button after registration
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish(); // To close registered Activity*/

                                    } else {
                                        Toast.makeText(Register.this, "Registeration fail, Please try again", Toast.LENGTH_SHORT).show();

                                    }
                                    loadingBar.dismiss();
                                }
                            });
                        } else {
                            try {
                                throw task.getException();
                            } catch (FirebaseAuthWeakPasswordException e) {
                                Password.setError("Your password is too weak");
                                Password.requestFocus();
                            } catch (FirebaseAuthInvalidCredentialsException e) {
                                EmailId.setError("Email is already in Registered");
                                EmailId.requestFocus();
                            } catch (FirebaseAuthUserCollisionException e) {
                                Name.setError("User is already registered.");
                                Name.requestFocus();
                            } catch (Exception e) {
                                Log.e(TAG, e.getMessage());
                                Toast.makeText(Register.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                            }
                            loadingBar.dismiss();
                        }
                    }
                });


    }
}
![Onthe firebase I am getting this misplaced](https://i.stack.imgur.com/IRfKS.png)

Please help why this is happening why the use details not showing on its correct place.your text

0 Answers
Related