Update data by matching a Parent node - Firebase Database - android

I want to update my password value, I will input the username and search for a parent node that would match the username text to change the password.
Here's my code
db = FirebaseDatabase.getInstance().getReference();
logIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressDialog.setMessage("Changing Password");
newPassword();
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Change successful.", Toast.LENGTH_LONG).show();
}
});//inside of onCreate method
private void newPassword(){
if(TextUtils.isEmpty(userText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input username.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(emailText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input email.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(contactText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input contact no.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(firstText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input first name.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(lastText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input last name.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input password.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(retype.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input retype password.", Toast.LENGTH_LONG).show();
return;
}
db.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String username = userText.getText().toString().trim();
for(DataSnapshot ds: dataSnapshot.getChildren()){
if(username.matches(ds.getKey())){
user.setPassword(password.getText().toString().trim());
ds.getRef().child(username).child("password").setValue(user.getPassword());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
}
My question is how to get to the node that I want to change its values, and every time I pressed the button, the password's value won't change.
Thanks for your help.
EDITED
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
...
progressDialog = new ProgressDialog(this);
db = FirebaseDatabase.getInstance().getReference();
db.child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
username = (String)dataSnapshot.child(userText.getText().toString().trim()).child("username").getValue();
address = (String)dataSnapshot.child(userText.getText().toString().trim()).child("address").getValue();
contact = (String)dataSnapshot.child(userText.getText().toString().trim()).child("contact").getValue();
email = (String)dataSnapshot.child(userText.getText().toString().trim()).child("email").getValue();
first = (String)dataSnapshot.child(userText.getText().toString().trim()).child("first").getValue();
last = (String)dataSnapshot.child(userText.getText().toString().trim()).child("last").getValue();
pass = passwordText.getText().toString().trim();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ForgotPassword.this, "Database Error", Toast.LENGTH_LONG).show();
Intent i = new Intent(ForgotPassword.this, SignIn.class);
startActivity(i);
}
});
logIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newPassword();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ForgotPassword.this, SignIn.class);
startActivity(i);
}
});
}
private void newPassword(){
...
user = new User(first, last, email, contact, pass, username, address);
db.child("users").child(username).setValue(user);
Toast.makeText(getApplicationContext(), "Change successful.", Toast.LENGTH_LONG).show();
Intent i = new Intent(this, TabMenu.class);
startActivity(i);
}
Every time I start this activity, it will give the error immediately on the onCancelled(DatabaseError){Toast.makeText(..., "Database Error", ...).show();}

If you want to compare two Strings in Java, you need to use equals method and not matches.
matches method tells whether or not this string matches the given regular expression.
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression
java.util.regex.Pattern.matches(regex, str).
So change this line of code:
username.matches(ds.getKey())
with
username.equals(ds.getKey())
To change the password, you only need to use setValue() method directly on the reference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("users").child("brian").child("password").setValue("newPassword");

Related

Firebase database when user try to register using onDataChange how to prevent from sending error toast

So when u try to register everything works great, when user register it show toast that user has successfully created and it also shows toast that username already exists. How can i prevent from showing toast that username already exists when user create his account, i guess it's the problem on onDataChange because it check always for that username is there any way i can do kinda better this, i'm new in android. Here is my register class.
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Common.isConnectedToInternet(getBaseContext())) {
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//Check if already exist username
if(dataSnapshot.child(edtUsername.getText().toString()).exists()) {
Toast.makeText(Register.this, "Username already exists", Toast.LENGTH_SHORT).show();
} else {
User user =
new User(edtUsername.getText().toString(),
edtPassword.getText().toString());
table_user.child(edtUsername.getText().toString()).setValue(user);
Toast.makeText(Register.this, "Successfully Registration", Toast.LENGTH_SHORT).show();
finish();
}
}
Use addListenerForSingleValueEvent
mDb.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
so it should be like this
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Common.isConnectedToInternet(getBaseContext())) {
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//Check if already exist username
if(dataSnapshot.child(edtUsername.getText().toString()).exists()) {
Toast.makeText(Register.this, "Username already exists", Toast.LENGTH_SHORT).show();
} else {
User user =
new User(edtUsername.getText().toString(),
edtPassword.getText().toString());
table_user.child(edtUsername.getText().toString()).setValue(user);
Toast.makeText(Register.this, "Successfully Registration", Toast.LENGTH_SHORT).show();
finish();
}
}

Not finishing in else part going back to if statement? (android and firebase)

I have method like below for signup scenario, After creating it will toast Account Created Successfully but after that again showing if part toast also like Phone Number Already Exists
private void CreateAccount(String name, String phone, String password) {
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
Toast.makeText(SignUpActivity.this, "All fields required", Toast.LENGTH_LONG).show();
} else {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("User");
final ProgressDialog dialog = new ProgressDialog(SignUpActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(edtPhone.getText().toString()).exists()) {
dialog.dismiss();
Toast.makeText(SignUpActivity.this, "Phone Number Already Exists", Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
table_user.child(edtPhone.getText().toString()).setValue(user);
Toast.makeText(SignUpActivity.this, "Account Created Successfully", Toast.LENGTH_SHORT).show();
Intent homeIntent = new Intent(SignUpActivity.this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(homeIntent);
finish();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
What did I do wrong can someone tell me whats happening and how to fix
this
Did you tried like this??
table_user.child(edtPhone.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() != null) {
dialog.dismiss();
Toast.makeText(SignUpActivity.this, "Phone Number Already Exists", Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
table_user.child(edtPhone.getText().toString()).setValue(user);
Toast.makeText(SignUpActivity.this, "Account Created Successfully", Toast.LENGTH_SHORT).show();
Intent homeIntent = new Intent(SignUpActivity.this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(homeIntent);
finish();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

Login Failed When Saving Data.(Sign-up)

I'm making an Android project, and for starters I have been figuring out how signing up works. By the way, I used Firebase for this. Here's my code:
package com....
import...
public class MainActivity extends AppCompatActivity {
//VIEW AND WIDGETS
Button createUser, moveToLoginBtn;
EditText userEmailEdit, userPasswordEdit;
//FIREBASE AUTH FIELDS
FirebaseAuth nAuth;
FirebaseAuth.AuthStateListener nAuthlistener;
DatabaseReference mDatabaseRef, mUserCheckData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ASSIGN ID
createUser = (Button) findViewById(R.id.createUserBtn);
moveToLoginBtn = (Button) findViewById(R.id.moveToLogin);
userEmailEdit = (EditText) findViewById(R.id.emailEditTextCreate);
userPasswordEdit = (EditText) findViewById(R.id.passEditTextCreate);
//ASSIGN INSTANCE
mDatabaseRef = FirebaseDatabase.getInstance().getReference();
mUserCheckData = FirebaseDatabase.getInstance().getReference().child("Users");
nAuth = FirebaseAuth.getInstance();
nAuthlistener = new FirebaseAuth.AuthStateListener(){
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
final String emailForVer = user.getEmail();
mUserCheckData.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
checkUserValidation(dataSnapshot, emailForVer);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
}
}
};
//ON CLICK LISTENER
createUser.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
final String userEmailString, userPassString;
userEmailString = userEmailEdit.getText().toString().trim();
userPassString = userPasswordEdit.getText().toString().trim();
if (!TextUtils.isEmpty(userEmailString) && !TextUtils.isEmpty(userPassString))
{
nAuth.createUserWithEmailAndPassword(userEmailString,userPassString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
DatabaseReference mChildDatabase = mDatabaseRef.child("Users").push();
String key_user = mChildDatabase.getKey();
mChildDatabase.child("isVerified").setValue("unverified");
mChildDatabase.child("userKey").setValue(key_user);
mChildDatabase.child("emailUser").setValue(userEmailString);
mChildDatabase.child("passWordUser").setValue(userPassString);
Toast.makeText(MainActivity.this, "User Account Created!", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, Profile.class));
}
else
{
Toast.makeText(MainActivity.this, "User Account Creation Fail", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
}
});
}
}
});
//MOVE TO LOGIN
moveToLoginBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
});
}
private void checkUserValidation(DataSnapshot dataSnapshot, String emailForVer) {
Iterator iterator = dataSnapshot.getChildren().iterator();
while (iterator.hasNext())
{
DataSnapshot dataUser = (DataSnapshot) iterator.next();
if(String.valueOf(dataUser.child("emailUser").getValue()).equals(emailForVer) && dataUser.child("emailUser") != null)
{
if(String.valueOf(dataUser.child("isVerified").getValue()).equals("unverified") && dataUser.child("isVerified") != null)
{
Intent in = new Intent(MainActivity.this, Profile.class);
in.putExtra("USER_KEY" , String.valueOf(dataUser.child("userKey").getValue()));
startActivity(in);
//in.putExtra("NAME_KEY" , String.valueOf(dataUser.child("nameKey").getValue()));
}else
{
startActivity(new Intent(MainActivity.this, Welcome.class));
}
}
}
}
#Override
protected void onStart() {
super.onStart();
nAuth.addAuthStateListener(nAuthlistener);
}
#Override
protected void onStop() {
super.onStop();
nAuth.removeAuthStateListener(nAuthlistener);
}
}
I think I have implemented my methods correctly. But it toasts:
User Account Creation Failed
Is my
checkUseValidation() method wrong? Any kind of help is appreciated.
Also please pay attention to my:
public void onComplete
method, I have set it right I think. I don't know why the data isn't getting saved to the firebase database. Or why is the task unsuccesful as show in line:
if (task.isSuccessful())
Thank you very much.
First, you should change the toast, in your fail condition, to
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
So that you get what wrong in the task.
But before that did you enable Email/Password in the console????
See Sign up new users from url: https://firebase.google.com/docs/auth/android/start/?authuser=0
If task.isSuccessful() is false.You can add this to get error
Log.w(TAG, "createUserWithEmail:failure",task.getException());
Update you code as:
createUser.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
final String userEmailString, userPassString;
userEmailString = userEmailEdit.getText().toString().trim();
userPassString = userPasswordEdit.getText().toString().trim();
if (!TextUtils.isEmpty(userEmailString) && !TextUtils.isEmpty(userPassString))
{
nAuth.createUserWithEmailAndPassword(userEmailString,userPassString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
DatabaseReference mChildDatabase = mDatabaseRef.child("Users").push();
String key_user = mChildDatabase.getKey();
mChildDatabase.child("isVerified").setValue("unverified");
mChildDatabase.child("userKey").setValue(key_user);
mChildDatabase.child("emailUser").setValue(userEmailString);
mChildDatabase.child("passWordUser").setValue(userPassString);
Toast.makeText(MainActivity.this, "User Account Created!", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, Profile.class));
}
else
{
Log.w("TAG", "createUserWithEmail:failure",task.getException());
Toast.makeText(MainActivity.this, "User Account Creation Fail", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
}
});
}
}
});
You should see warning in logcat:

How to separate two users in the firebase in android

In my application, I have two users--Event Member and Client--they have separate user login and registration. If a client log in he will go to the the client activity; if an event member will log in he will go to the event member activity. How will I make sure that the email is a client or an event member?
Below image shows my firebase structure:
Here is my code:
SignupClient.java
signupClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String cemail = clie_email.getText().toString().trim();
final String cpassword = clie_password.getText().toString().trim();
String ccpassword = clie_cpassword.getText().toString().trim();
final String cfname = clie_firstname.getText().toString().trim();
final String clname = clie_lastname.getText().toString().trim();
final String cbday = clie_birthday.getText().toString().trim();
final String ccountry = clie_country.getSelectedItem().toString();
final String cmobile = clie_mobile.getText().toString().trim();
auth.createUserWithEmailAndPassword(cemail, cpassword)
.addOnCompleteListener(_5_SignupClient.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(_5_SignupClient.this, "createUserWithEmail: onComplete" + task.isSuccessful(), Toast.LENGTH_LONG).show();
if (!task.isSuccessful()){
Toast.makeText(_5_SignupClient.this, "Authentication Failed" + task.getException(),
Toast.LENGTH_LONG).show();
}
else {
AccountInfo accountInfo = new AccountInfo(cfname, clname, cemail, cpassword, cbday, ccountry, cmobile);
mDatabaseReference.child("client").push().setValue(accountInfo);
startActivity(new Intent(_5_SignupClient.this, _7_ViewClient.class));
finish();
}
}
});
}
});
LoginClient.java
loginClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String clie_unameemail = clie_emailuname.getText().toString();
final String clie_pass = clie_password.getText().toString();
if(TextUtils.isEmpty(clie_unameemail)){
Toast.makeText(getApplicationContext(), "Field cannot be empty", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(clie_pass)){
Toast.makeText(getApplicationContext(), "Field cannot be empty", Toast.LENGTH_LONG).show();
return;
}
auth.signInWithEmailAndPassword(clie_unameemail, clie_pass)
.addOnCompleteListener(_3_LoginClient.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
ref = FirebaseDatabase.getInstance().getReference().child("client");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
if(FirebaseAuth.getInstance().getCurrentUser().getUid().equals(snapshot.getKey())){
startActivity(new Intent(_3_LoginClient.this, _7_ViewClient.class));
}
}
// startActivity(new Intent(_3_LoginClient.this, Normal_memberActivity.class));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
// User is signed out
}
// ...
}
};
if (!task.isSuccessful()) {
// there was an error
if (clie_pass.length() < 8) {
clie_password.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(_3_LoginClient.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(_3_LoginClient.this, "Successfully Registered", Toast.LENGTH_LONG).show();
Intent intent = new Intent(_3_LoginClient.this, _7_ViewClient.class);
startActivity(intent);
finish();
}
}
});
}
});
I hope you could help me. Thank you!
On your db there should be one more field like we say it USER_TYPE. While registering the user send its USER_TYPE. suppose if you are registering a user as a CLIENT then inser db value USER_TYPE="CLIENT" and if its as an Event member registration then inser db value USER_TYPE="EVENT" and now once you logged in check its USER_TYPE and redirect him based upon his USER_TYPE

I need to wait until listener changes boolean from null to false/true java

I have a Firebase listener that checks if User exists (Im making login/signup using Firebase database) in order to tell if the username is taken or not, the problem is that it takes 2 clicks on the Signup button for it to work, because the listener cant tell if username exists or not fast enough, only on second click when it already decided for the first click it is possible to signup, but then again it does not really check the username of the second click (If I change username now, even if it is taken it will work)
database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users/" + usernameEt.getText().toString() +"/password");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
if(value != null) {
Toast.makeText(getApplicationContext(), "Username Taken", Toast.LENGTH_SHORT).show();
clear = false;
}else
clear = true;
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
clear = false;
Toast.makeText(getApplicationContext(), "Internet Error", Toast.LENGTH_SHORT).show();
}
});
//insert check if clear has value
if(!clear) //TODO FIX takes time to the listener to do the job
return false;
clear is a Boolean type var and is null at the first time this code runs, this code is for checking if username is taken or not
Maybe your code looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
...
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkUserExists()) {
...
} else {
signUp();
}
}
}
}
private boolean checkUserExists() {
database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users/" + usernameEt.getText().toString() +"/password");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
if(value != null) {
Toast.makeText(getApplicationContext(), "Username Taken", Toast.LENGTH_SHORT).show();
clear = false;
}else
clear = true;
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
clear = false;
Toast.makeText(getApplicationContext(), "Internet Error", Toast.LENGTH_SHORT).show();
}
});
//insert check if clear has value
if(!clear) //TODO FIX takes time to the listener to do the job
return false;
}
private void signUp() {
...
}
You should change it to:
#Override
public void onCreate(Bundle savedInstanceState) {
...
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkUserExists();
}
}
}
private boolean checkUserExists() {
database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users/" + usernameEt.getText().toString() +"/password");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
if(value != null) {
Toast.makeText(getApplicationContext(), "Username Taken", Toast.LENGTH_SHORT).show();
} else {
signUp(); // sign up here
}
}
#Override
public void onCancelled(DatabaseError error) {
Toast.makeText(getApplicationContext(), "Internet Error", Toast.LENGTH_SHORT).show();
}
});
}
private void signUp() {
...
}
Also you can block user input while checking user data:
private boolean checking = false; // added
#Override
public void onCreate(Bundle savedInstanceState) {
...
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checking) return; // added
checkUserExists();
checking = true; // added
}
}
}
private boolean checkUserExists() {
database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users/" + usernameEt.getText().toString() +"/password");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
checking = false; // added
String value = dataSnapshot.getValue(String.class);
if(value != null) {
Toast.makeText(getApplicationContext(), "Username Taken", Toast.LENGTH_SHORT).show();
} else {
signUp(); // sign up here
}
}
#Override
public void onCancelled(DatabaseError error) {
checking = false; // added
Toast.makeText(getApplicationContext(), "Internet Error", Toast.LENGTH_SHORT).show();
}
});
}
private void signUp() {
...
}
Hope it works.

Categories

Resources