I want to add a onClickListener to navigation view header's items like changing a TextView text or set an image for ImageView.
In your activity_main.xml layout add navigation drawer code.
<LinearLayout
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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    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="match_parent">
    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></FrameLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/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:layout_marginTop="-24dp"
        app:headerLayout="@layout/drawer_header"
        app:itemTextColor="@color/black"
        app:menu="@menu/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
and adder your drawer_header.xml in the layout directory
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
    android:clickable="true"
    android:paddingTop="20dp"
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My header"
    android:textAppearance="?android:attr/textAppearanceLarge"/>
add nav_menu.xml to your menu directory
<?xml version="1.0" encoding="utf-8"?>
<group
    android:id="@+id/main_features"
    android:checkableBehavior="single">
    <item
        android:id="@+id/idHome"
        android:title="Home" />
    <item
        android:id="@+id/favorite"
        android:title="Favorites" />
   <item
        android:id="@+id/idSettings"
        android:title="Settings" />
</group>
in your MainActivity.java listen to the clicks
 DrawerLayout mDrawerLayout;
 NavigationView mNavigationView;
 TextView header;
then initialize them in your onCreate():
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
 mNavigationView = (NavigationView) findViewById(R.id.navigationView);
  header = (TextView) findViewById(R.id.textView5);
    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"Calling Header", Toast.LENGTH_LONG).show();
        }
    });
Update: 27/10/2015
with the update on support library adding header to the navigation view in xml is not working, may be its a bug.
to add a header view inflate the view as navigation view header in java.
mNavigationView.inflateHeaderView(R.layout.layout_header_profile);
hope it will help. Happy Coding...
You can do this -
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
or if you have multiple headers:
navigationView.getHeaderCount();
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With