• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to display username and email from firebase in android

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.Query;
import com.google.firebase.database.ValueEventListener;
import com.s.attendancemanagementsystem.R;

public class ProfileActivity extends AppCompatActivity {

   private static final String TAG = ProfileActivity.class.getSimpleName();
   Button logout;
   ImageView profilePic;
   private static int PICK_IMAGE = 500;
   private Uri imagePath;
   private TextView username_profile,user_email;
   private FirebaseAuth firebaseAuth;
   private DatabaseReference databaseReference;
   private FirebaseUser firebaseUuser;
   String uId;
   protected void onCreate(Bundle savedInstanceState) {

       username_profile = findViewById(R.id.username_profile_student);
       user_email = findViewById(R.id.email_profile_student);
       //initializing firebase authentication object
       firebaseAuth = FirebaseAuth.getInstance();

       databaseReference = FirebaseDatabase.getInstance().getReference();

       Query q = databaseReference.child("users").orderByChild("email").equalTo(firebaseAuth.getCurrentUser().getEmail());
       Log.v(TAG, "My Query is here" + q);
       q.addListenerForSingleValueEvent(new ValueEventListener() {
           public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
               for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                   if (postSnapshot.getValue() != null) {
                       Log.v(TAG, "DataSnaptshoot is here" + postSnapshot);
                       String value = dataSnapshot.getValue(String.class);

                       User user= dataSnapshot.child(uId).getValue(User.class);
                       String name = user.getName();
                       String email = user.getEmail();
                       username_profile.setText("User name: "+name);

           public void onCancelled(@NonNull DatabaseError databaseError) {

       //displaying logged in user name
      // username_profile = findViewById(R.id.username_profile_student);
       //user_email = findViewById(R.id.email_profile_student);

       //user_email.setText("Email: "+user.getEmail());

       logout = findViewById(R.id.btnLogout);
       profilePic =findViewById(R.id.profile_pic);

       logout.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               //if logout is pressed
               if (v == logout) {
                   //logging out the user
                   //closing activity
                   //starting login activity
                   startActivity(new Intent(ProfileActivity.this, StudentLogin.class));

       profilePic.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {


   private void  OpenGallery()
       Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   { super.onActivityResult(requestCode,resultCode,data);

       if(resultCode == RESULT_OK && requestCode == PICK_IMAGE)
           imagePath = data.getData();

Saloon Keeper
Posts: 7597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot of code, which is barely legible without CODE tags. What does it do, what should it be doing instead, and what problems do you have making the required changes? TellTheDetails
Posts: 665
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross post : https://stackoverflow.com/questions/55561014/how-to-display-username-and-email-from-firebase-in-android/55567834#55567834
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic