• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to display username and email from firebase in android

 
Greenhorn
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;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_profile);

       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() {
           @Override
           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);
                      username_profile.setText(value);
                      user_email.setText(value);

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

           @Override
           public void onCancelled(@NonNull DatabaseError databaseError) {
               Toast.makeText(ProfileActivity.this,databaseError.getCode(),Toast.LENGTH_LONG).show();
           }
       });


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


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


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

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

       profilePic.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {
               OpenGallery();

           }
       });

   }
   //=============Gallery===Code=for=Image===========================================================
   private void  OpenGallery()
   {
       Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
       startActivityForResult(gallery,PICK_IMAGE);
   }

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

       if(resultCode == RESULT_OK && requestCode == PICK_IMAGE)
       {
           imagePath = data.getData();
           profilePic.setImageURI(imagePath);
       }
   }

   //================================================================================================
}
 
Saloon Keeper
Posts: 7597
177
  • 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
 
Rancher
Posts: 665
10
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.
reply
    Bookmark Topic Watch Topic
  • New Topic