NavigationView get/find header layout

In my NavigationView I have a header layout with id ‘viewId’ with active buttons. To setup those buttons, I do the following in activity’s onPostCreate:

final View panel = findViewById(R.id.viewId);
panel.setOnClickListener(new View.OnClickListener() {
... setup goes here ...
});

With new version android support library, (23.1.0), the view can’t be found, it returns null. With previous versions it worked well. Is it a bug or am I using this feature wrong? If so, how to access header layout and add behavior to it?

  • Why should we use sp for font sizes in Android?
  • Is there a faster video rendering solution than using AndroidBitmap_xxx functions?
  • Listview with edittext - auto scroll on “next”
  • SQLiteConstraintException not caught
  • Connection pool has been unable to grant a connection to thread
  • Animation-list not working - shows only the first element
  • Related posts:

    Windows 8 - Running Eclipse (Java VM not found)
    device does not have package com.google.android.gsf in google nexus phone
    Why GreenDAO doesn't support LIKE operator completely?
    Transfer real-time video stream to server using Android
    android - disable listview item click and reenable it
    Android S3 layout vs Galaxy Nexus layout
  • How to prevent other View being pushed out of the screen
  • Retrieve a Fragment from a ViewPager
  • Phonegap/HTML5 and Android screen resize issue on rotate from landscape to portrait
  • How to read an SQLite DB in android with a cursorloader?
  • Rebuild, Sync Gradle and Clean for just one Module with Android Studio
  • Gridview disappear?
  • 4 Solutions collect form web for “NavigationView get/find header layout”

    Version 23.1.0 switches NavigationView to using a RecyclerView (rather than the previous ListView) and the header is added as one of those elements. This means it is not instantly available to call findViewById() – a layout pass is needed before it is attached to the NavigationView.

    For version 23.1.1 of the Support Library, you can now get a reference to the header view using getHeaderView():

    View headerLayout = navigationView.getHeaderView(0); // 0-index header
    

    This has the advantage of working on headers added via XML and via code.

    If you are still using 23.1.0, as per the related bug, you can inflate the header in code and use findViewById() on that:

    View headerLayout = 
        navigationView.inflateHeaderView(R.layout.navigation_header);
    panel = headerLayout.findViewById(R.id.viewId);
    // panel won't be null
    

    Until you move to 23.1.1.

    Now with the 23.1.1 release of the design support library, you can use

    NavigationView navigationView = (NavigationView) findViewById(R.id.your_nav_view_id);
    View header = navigationView.getHeaderView(0)
    TextView text = (TextView) header.findViewById(R.id.textView);
    

    For me that was the same situation with 23.1.0, after of update the null pointer exception become. In this case the NavigatorView look like:

      <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_nav"
         />
    

    I tried the solution proposal by ianhanniballake but it does not work. Then I inflated with the sentence:

     LayoutInflater.from(getContext()).inflate(R.layout.nav_header, mNavigationView);
    

    After that, I can find by id all views defined in nav_heard.

    This is how I did it using ButterKnife and it works for me.

    protected static class HeaderViewHolder {
    
        @BindView(R.id.button)
        Button button;
    
        HeaderViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    

    and then use this view holder like this :

    View header = navigationView.getHeaderView(0);
    headerViewHolder = new HeaderViewHolder(header);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.