3

I was able to make a navigation drawer and google sign in app using firebase tutorials from youtube https://www.youtube.com/playlist?list=PLGCjwl1RrtcR1j6EmpBxJyJYowK2QIsdT

I have activity_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="rjbc.cpvc.NavigationActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout = "@layout/navigation_action"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:text="TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:layout_marginStart="101dp"
                android:layout_marginTop="201dp"
                android:id="@+id/textView5" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_person_black_24dp"
                android:layout_below="@+id/textView5"
                android:layout_alignEnd="@+id/textView5"
                android:layout_marginEnd="42dp"
                android:layout_marginTop="53dp"
                android:id="@+id/imageView" />
        </RelativeLayout>
    </LinearLayout>    

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity = "start"
        app:headerLayout="@layout/navigation_header" />

</android.support.v4.widget.DrawerLayout>

and navigation_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="140dp"
    android:background="@color/colorPrimary"
    android:padding="20dp"
    android:id="@+id/pName">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="13dp"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:textColor="@color/common_google_signin_btn_text_dark_default" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_person_black_24dp"
        android:id="@+id/pPic"
        android:layout_above="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="18dp" />
</RelativeLayout>

this is my navigation_activity.java

public class NavigationActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private TextView mName;

    private String name;
    private Uri photoUrl;
    private ImageView mPic;

    private NavigationView mNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        mToolbar = findViewById(R.id.nav_action);
        setSupportActionBar(mToolbar);
        mDrawerLayout = findViewById(R.id.drawerLayout);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if(firebaseAuth.getCurrentUser() == null) {
                    startActivity(new Intent(NavigationActivity.this,MainActivity.class));
                }
            }
        };

        mName = mNavigationView.getHeaderView(0).findViewById(R.id.textView);
        mPic = mNavigationView.getHeaderView(0).findViewById(R.id.pPic);
        getCurrentinfo();

        mNavigationView = findViewById(R.id.nav_view);

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(final MenuItem menuItem) {

                int id = menuItem.getItemId();
                switch (id) {
                    case R.id.nav_account:
                        Toast.makeText(NavigationActivity.this, "Account Selected", Toast.LENGTH_SHORT).show();
                        return true;

                    case R.id.nav_settings:
                        Toast.makeText(NavigationActivity.this, "Setting Selected", Toast.LENGTH_SHORT).show();

                    default:
                        return true;
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void getCurrentinfo() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null) {
            for (UserInfo profile : user.getProviderData()) {
                // Id of the provider (ex: google.com)
                String providerId = profile.getProviderId();

                // UID specific to the provider
                String uid = profile.getUid();

                // Name, email address, and profile photo Url
                name = profile.getDisplayName();
                photoUrl = profile.getPhotoUrl();
                mName.setText(name);

                Picasso.with(getApplicationContext())
                        .load(photoUrl.toString())
                        .placeholder(R.drawable.logo)
                        .resize(100, 100)
                        .transform(new CircleTransform())
                        .centerCrop()
                        .into(mPic);    
            }
        }
    }
}

in my getcurrentInfo function I am able to place my name and profile pic in the activity_navigation.xml but I want to place it an navigation_header.xml

I tried changing the id of my ImageView and TextView to match with that in the navigation_header but my app crashes.

3 Answers 3

4

Try this after mNavigationView = (NavigationView) findViewById(R.id.nav_view);

TextView userName = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.username);

It is because you are initializing the textview before the navigationview get initialized

mName   = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textView);
    mPic = (ImageView) mNavigationView.getHeaderView(0).findViewById(R.id.pPic);
        getCurrentinfo();

        mNavigationView = (NavigationView) findViewById(R.id.nav_view); //navigation view is initialized here
1
  • Then please mark it as answer it will be glad to others if you marked it who are seeking the same one @Dr.DOOM Feb 15, 2017 at 8:37
4

This is going to work 100% hopefully. Let me know if it works.

`

    //Initializing firebase objects

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseUser = firebaseAuth.getCurrentUser();

   //Go to your specific database directory or Child
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Students").child(firebaseUser.getUid());

   //Connect the views of navigation bar
    mNavigationView = (NavigationView) findViewById(R.id.nav_view);
    mName   = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textViewHeaderTitle);
    mID   = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textViewHeaderSID);
    mImageView = (CircleImageView) mNavigationView.getHeaderView(0).findViewById(R.id.imageViewHeaderIcon);

    //Use you DB reference object and add this method to access realtime data
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

           //Fetch values from you database child and set it to the specific view object.
            mName.setText(dataSnapshot.child("name").getValue().toString()); 
            mID.setText(dataSnapshot.child("sid").getValue().toString());  

            String link =dataSnapshot.child("profile_picture").getValue().toString();
            Picasso.with(getBaseContext()).load(link).into(mImageView);   
        }

        //SIMPLE BRO. HAVE FUN IN ANDROID <3 GOOD LUCK

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

`

3
  • had a problem and this worked, but just for clarification, the getHeaderViews, it gets the views in the header of a nav drawer.. but how are they are in the same index? Oct 22, 2017 at 21:01
  • 1
    @PeterHaddad I don't know that part as well XD, I just took that part from the guy who asked this question. It was like his problem was what I already solved. But my problem was what he already solved. ha ha.. I was also unable to access the navigation drawer but I knew how to get images and text and set them. Let me know if you find out the reason. that would be a great help bro. Thank you.
    – Tamim
    Oct 25, 2017 at 7:23
  • 1
    Yes, the getHeaderViews, gets the views that are defined in the header navigation drawer, and you use mNavigationView.findViewByid since the textview is a child of the navigationview. For the index, its 0 since its defined in layout Oct 25, 2017 at 7:42
3

From NavigationView you will access the textview and imageview with "navigationView.getHeaderView(0)" this line

For Quick Sample

TextView userName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.username);
    userName.setText(new UserPreference().getUser(MainActivity.this).userName);

All the Best!!

1
  • Can you try to update your code, so i will look at it Feb 15, 2017 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.