I have this activity in which I want to authorize the confirm password field.This is my code-:
package com.example.loginregisterfirebase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.lang.ref.Reference;
public class Register extends AppCompatActivity {
//create object of DatabaseReference class to access firebase's Realtime Database
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://loginregisterfirebase-9fe2f-default-rtdb.firebaseio.com/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText fullname = findViewById(R.id.fullname);
final EditText email = findViewById(R.id.email);
final EditText phone = findViewById(R.id.phone);
final EditText password = findViewById(R.id.password);
final EditText conPassword = findViewById(R.id.conPassword);
final Button registerBtn = findViewById(R.id.registrationBtn);
final TextView loginNowBtn = findViewById(R.id.loginNowBtn);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//get data from EditTexts into String variables
final String fullnameTxt = fullname.getText().toString();
final String emailTxt = email.getText().toString();
final String phoneTxt = phone.getText().toString();
final String passwordTxt = password.getText().toString();
final String conPasswordTxt = conPassword.toString().toString();
// check if user fill all the fields before sending data
if (fullnameTxt.isEmpty()||emailTxt.isEmpty()||phoneTxt.isEmpty()||passwordTxt.isEmpty()||conPasswordTxt.isEmpty()){
Toast.makeText(Register.this, "please fill the all the fields", Toast.LENGTH_SHORT).show();
}
//check if passwords are matching with each other
//if not matching with each other then show a toast message
else if (passwordTxt.equals(conPasswordTxt)){
Toast.makeText(Register.this, "Passwords do NOT match", Toast.LENGTH_SHORT).show();
}
else{
databaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//check if phone number is registered before
if (snapshot.hasChild(phoneTxt)){
Toast.makeText(Register.this, "Phone number is already registered", Toast.LENGTH_SHORT).show();
}
else {
// storing or sending data to firebase Realtime Database.
//we are using phone number as unique identity of every user
//so all the other details of user comes under phone number
databaseReference.child("users").child(phoneTxt).child("fullname").setValue(fullnameTxt);
databaseReference.child("users").child(phoneTxt).child("email").setValue(emailTxt);
databaseReference.child("users").child(phoneTxt).child("password").setValue(passwordTxt);
//show success message than finish activity
Toast.makeText(Register.this, "User registered successfully", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
});
loginNowBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
if I eleminate this section the application is working properly.:-
else if (passwordTxt.equals(conPasswordTxt)){ Toast.makeText(Register.this, "Passwords do NOT match", Toast.LENGTH_SHORT).show(); }
but I need this section anyhow... Now even after I have both of the EditText fields to be same it is still showing me the Toast that passwords do not match. Please help me out here. Thanks in advance.
CodePudding user response:
final String conPasswordTxt = conPassword.toString().toString();
This should be final String conPasswordTxt = conPassword.getText().toString();