Navigationview Get/Find Header Layout

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.

android - How to access the Navigation Drawer Header Items?

You need to inflate the header view as it is not inflated automatically .

View header = mNavigationView.getHeaderView(0);
mNameTextView = (TextView) header.findViewById(R.id.nameTextView);
mNameTextView.setText("XYZ");

NavigationView how to handle dynamic header content

You can do that in code by inflating custom layout and set it's header for navigation view.

NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
View nav_header = LayoutInflater.from(this).inflate(R.layout.nav_header, null);
((TextView) nav_header.findViewById(R.id.name)).setText("UserName");
navigationView.addHeaderView(nav_header);

You don't need to set app:headerLayout in xml.

Header Repetition in navigation view android

Update your support library to 23.1.1 or above.

After which you can do this -

Add the headerview in the app:headerLayout="@layout/header" inside NavigationView.

Then, you can access it by,

    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
View headerView = mNavigationView.getHeaderView(0)

googele_plus = (ImageButton) headerView.findViewById(R.id.google_p);
facebook = (ImageButton) headerView.findViewById(R.id.fb);

Ref : https://code.google.com/p/android/issues/detail?id=190226#c31

Home Navigation header of NavigationDrawer not showing in simulator

With help from @MikeM., I was able to adjust my layout settings in a way that allowed me to see the navigation header.

His contribution can be read directly here -
Home Navigation header of NavigationDrawer not showing in simulator

How to get view from drawer header layout with binding in activity?

Updated solution (13/11/2015)

Solution: Update your Design Support Library to 23.1.1:

Changes for Design Support library 23.1.1:

  • Added the getHeaderView method to the NavigationView class.
  • Fixed a transparent background issue for a FloatingActionButton object on devices running Android 4.0 (API level 15) and lower. (Issue 183315)

See https://developer.android.com/tools/support-library/index.html for more info



Original solution

I don't know why there is no method which provides header view
attached programmatically.

Instead, here's two solutions:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView iv = (ImageView)headerview.findViewById(R.id.your_image_view)

Or:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);
navigationView.addHeaderView(headerView);

ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

How can I edit the header view element in Navigation Drawer programatically?

You can simply retrieve your View using

mNavigationView.findViewById(R.id.IDVIEWTOUPDATE)


Related Topics



Leave a reply



Submit