title: checking the email and username availability(Firebase) in Android Studio
body: <p>I'm trying to build a signup system with the help of Firebase Realtime Database in Android Studio. So far the signup does not require a unique username and email. This is how I validate the users' input.</p>
  <pre><code> private Boolean validateUsername() {
         String val = regUsername.getEditText().getText().toString();
          String noWhiteSpace = &quot;\\A\\w{4,20}\\z&quot;;
                  if (val.isEmpty()) {
                      regUsername.setError(&quot;Field cannot be empty&quot;);
                      return false;
                  }
                   else if (val.length() &gt;= 15) {
                      regUsername.setError(&quot;Username too long&quot;);
                      return false;
                  } else if (!val.matches(noWhiteSpace)) {
                      regUsername.setError(&quot;Spaces are not allowed&quot;);
                      return false;
                  } else {
                      regUsername.setError(null);
                      regUsername.setErrorEnabled(false);
                      return true;
                  }
     }
      private Boolean validateEmail() {
          String val = regEmail.getEditText().getText().toString();
          String emailPattern = &quot;[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+&quot;;
          if (val.isEmpty()) {
              regEmail.setError(&quot;Field cannot be empty&quot;);
              return false;
          } else if (!val.matches(emailPattern)) {
              regEmail.setError(&quot;Invalid email address&quot;);
              return false;
          } else {
              regEmail.setError(null);
              regEmail.setErrorEnabled(false);
              return true;
          }
      }
  </code></pre>
  <p>As shown I'm using Boolean functions for that and When the signup button is clicked, the below code executes.</p>
  <pre><code>  public void registerUser(View view) {
          //Performing Validation by calling validation functions
          if (!validateName() | !validatePassword() | !ConfirmPassword() | !validateEmail() | !validateUsername()) {
              return;
          }
          rootNode = FirebaseDatabase.getInstance();
          reference = rootNode.getReference(&quot;users&quot;);
          String name = regName.getEditText().getText().toString();
          String username = regUsername.getEditText().getText().toString();
          String email = regEmail.getEditText().getText().toString();
          String password = regPassword.getEditText().getText().toString();
          UserHelperClass helperClass = new UserHelperClass(name, username, email, password);
          reference.child(username).setValue(helperClass);
          Intent intent = new Intent(SignUp.this, UserProfile.class);
          startActivity(intent);
      }
  </code></pre>
  <p>I have searched on the internet on how to check whether the username and email already exist or not and I found this method.</p>
  <pre><code> DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  ref.child(&quot;users&quot;).child(&quot;username&quot;).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       if(dataSnapshot.exists()){
        // use &quot;username&quot; already exists
      } else {
        // &quot;username&quot; does not exist yet.
      }                               
    }
  
    @Override
    public void onCancelled(DatabaseError databaseError) {
  
    }
  }); 
  </code></pre>
  <p>The problem is that the function is void and I want it to be boolean so I can add it as a condition inside validateUsername() &amp; validateEmail(). How can I do that?</p>