2

I have a image and a text in my Header layout which is included through android.support.design.widget.NavigationView. The picture will be user's profile picture and text will be name which I'll get from Session Manager Class.

How can I access and edit the text and image programmatically, since it's only included in the definition of NavigationView.

Here's the code:

 <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/grey"
        app:menu="@menu/drawermenu"
        app:headerLayout="@layout/headerlayout"
        app:itemIconTint="@color/colorPrimary"
        android:layout_marginTop="0dp"

        />

And in activity class:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

    /**
     * Lets inflate the very first fragment
     * Here , we are inflating the TabFragment as the first Fragment
     */

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new helpfragment()).commit();
    /**
     * Setup click events on the Navigation View Items.
     */

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


            if (menuItem.getItemId() == R.id.nav_item_home) {
                Intent i = new Intent(help.this, home.class);
                startActivity(i);
            }



            if (menuItem.getItemId() == R.id.nav_item_tenant) {
                Intent i = new Intent(help.this, rentertabview.class);
                startActivity(i);
            }
            if (menuItem.getItemId() == R.id.nav_item_profile) {
                Intent i = new Intent(help.this, profile.class);
                startActivity(i);
            }


            if (menuItem.getItemId() == R.id.nav_item_owner) {
                Intent i = new Intent(help.this, ownertabview.class);
                startActivity(i);
            }

            if (menuItem.getItemId() == R.id.nav_item_services) {
                Intent i = new Intent(help.this, services.class);
                startActivity(i);
            }

            return false;
        }

    });

    /**
     * Setup Drawer Toggle of the Toolbar
     */

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
            R.string.app_name);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerToggle.syncState();

Please explain in details. Will be really thankful to you.

Basu
  • 763
  • 8
  • 27
  • I've basically already answered that [here](http://stackoverflow.com/a/33365230/1029225). The latest version of the design support library has a method called `getHeaderView(int)` which you can use to retrieve the header view(s). From there you can do your normal `findViewById()` and update the relevant views with the profile data. If you're not using the latest version of the library, it's probably best to update. If that's not possible for whatever reason, please mention which version you *are* using, because the correct approach will depend on that. – MH. Nov 27 '15 at 15:46
  • It worked! Thanks a lot. – Basu Nov 27 '15 at 17:03
  • Cool, glad to hear. I've closed the question for now as a dupe because there are already various answers providing a solution floating around. – MH. Nov 27 '15 at 18:55

1 Answers1

0

You can simply retrieve your View using

mNavigationView.findViewById(R.id.IDVIEWTOUPDATE)
GGC
  • 102
  • 5
  • Have you tried this on the latest version of the design support library? Just asking because I can promise you a `null` value if you do. ;) [See also here](http://stackoverflow.com/questions/33364276/getting-error-in-existing-code-after-updating-support-repository-to-23-1-0). – MH. Nov 27 '15 at 16:14
  • I'm using it with design support library – GGC Nov 30 '15 at 10:31
  • And now update to `23.1.1` (or `23.1.0` for that matter) and see what happens. Key to my previous comment was "***on the latest version** of the design support library*". – MH. Nov 30 '15 at 10:33
  • 1
    use this : View headerView = navigationView.getHeaderView(0); AppCompatTextView profileName = (AppCompatTextView) headerView.findViewById(R.id.tv_name_navDrawer); – Abhishek Sengupta Aug 17 '18 at 12:16