title: How to display username Firebase in android
body: <p>I have done a lot of ways to only show the username of the firebase database. but i am getting null values . maybe from you can help me to solve this problem. </p>
  
  <p>this is the database I have.
  <a href="https://i.stack.imgur.com/bgcgI.png" rel="nofollow noreferrer">My Firebase Database</a>.</p>
  
  <p>then this is the activity to display the username
  <a href="https://i.stack.imgur.com/mx4ES.png" rel="nofollow noreferrer">Activity For Display Username</a></p>
  
  <p>This is the animation I have created so that you can more easily understand it.
  <a href="https://i.stack.imgur.com/nmkbJ.gif" rel="nofollow noreferrer">Animated GIF</a></p>
  
  <p>This is my code</p>
  
  <p><strong>FirstActivity.Java</strong></p>
  
  <pre><code>public class FirstActivity extends AppCompatActivity {
  
  TextView textView;
  FirebaseDatabase database;
  DatabaseReference reference;
  Toolbar toolbar;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_first);
  
      toolbar = findViewById(R.id.my_toolbar);
      setSupportActionBar(toolbar);
  
      database = FirebaseDatabase.getInstance();
      textView = findViewById(R.id.tvselamatdatang);
      reference = database.getReference("Users");
      reference.addValueEventListener(new ValueEventListener() {
          @SuppressLint("SetTextI18n")
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              String username = dataSnapshot.child("username").getValue(String.class);
              textView.setText("Welcome" + username);
          }
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  }
  }
  </code></pre>
  
  <p><strong>LoginActivity.Java</strong></p>
  
  <pre><code>public class LoginActivity extends AppCompatActivity {
  
  FirebaseDatabase database;
  DatabaseReference users;
  
  ProgressBar progressBar;
  EditText editTextUsername, editTextPassword;
  Button buttonLogin;
  TextView textViewSignUp;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
  
      database = FirebaseDatabase.getInstance();
      users = database.getReference("Users");
      progressBar = (ProgressBar) findViewById(R.id.progressbar);
  
      editTextUsername = (EditText) findViewById(R.id.editTextUsername);
      editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  
      buttonLogin = (Button) findViewById(R.id.buttonLogin);
      buttonLogin.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              signIn(editTextUsername.getText().toString(),
                      editTextPassword.getText().toString());
          }
      });
  
      textViewSignUp = (TextView) findViewById(R.id.textViewSignup);
      textViewSignUp.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
              startActivity(intent);
          }
      });
  
  }
  
  private void signIn(final String username, final String password) {
      users.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              if(dataSnapshot.child(username).exists()){
                  if (!username.isEmpty()){
                      User login = dataSnapshot.child(username).getValue(User.class);
                      if (login.getPassword().equals(password)){
                          Toast.makeText(LoginActivity.this, "Success Login", Toast.LENGTH_SHORT).show();
                          Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
                          startActivity(intent);
                      }
                      else {
                          Toast.makeText(LoginActivity.this, "Password is Wrong", Toast.LENGTH_SHORT).show();
                      }
                  }
                  else Toast.makeText(LoginActivity.this, "Username is not Registered", Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  }
  }
  </code></pre>
  
  <p><strong>SignUpActivity.Java</strong></p>
  
  <pre><code>public class SignUpActivity extends AppCompatActivity {
  
  FirebaseDatabase database;
  DatabaseReference users;
  
  ProgressBar progressBar;
  EditText editTextEmail, editTextUsername, editTextPassword;
  Button btnSignUp;
  TextView textViewLogin;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sign_up);
  
      database = FirebaseDatabase.getInstance();
      users = database.getReference("Users");
  
      progressBar = (ProgressBar) findViewById(R.id.progressbar);
  
      editTextEmail = (EditText) findViewById(R.id.editTextEmail);
      editTextUsername = (EditText) findViewById(R.id.editTextUsername);
      editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  
      textViewLogin = (TextView) findViewById(R.id.textViewLogin);
      textViewLogin.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
              startActivity(intent);
          }
      });
  
      btnSignUp = (Button) findViewById(R.id.buttonSignUp);
  
      btnSignUp.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              final User user = new User(editTextUsername.getText().toString(),
                      editTextPassword.getText().toString(),
                      editTextEmail.getText().toString());
              users.addListenerForSingleValueEvent(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
                      if (dataSnapshot.child(user.getUsername()).exists())
                          Toast.makeText(SignUpActivity.this, "The Username is Already Exist!", Toast.LENGTH_SHORT).show();
                      else {
                          users.child(user.getUsername()).setValue(user);
                          Toast.makeText(SignUpActivity.this, "Success Register!", Toast.LENGTH_SHORT).show();
                      }
                  }
  
                  @Override
                  public void onCancelled(DatabaseError databaseError) {
  
                  }
              });
          }
      });
  
  }
  }
  </code></pre>
  
  <p><strong>User.Java</strong></p>
  
  <pre><code>public class User {
  
  private String username;
  private String password;
  private String email;
  
  public User() {
  
  }
  
  public User(String username, String password, String email) {
      this.username = username;
      this.password = password;
      this.email = email;
  }
  
  public String getUsername() {
      return username;
  }
  
  public void setUsername(String username) {
      this.username = username;
  }
  
  public String getPassword() {
      return password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  public String getEmail() {
      return email;
  }
  
  public void setEmail(String email) {
      this.email = email;
  }
  }
  </code></pre>