i know its late this is for those who facing the same problem.
place your header layout in the navigation view like this
this is in activity_main.xml
<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:itemTextColor="@color/black"
app:headerLayout="@layout/layout_header_profile"
app:menu="@menu/nav_menu"/>
create a layout, name it layout_header_profile.xml and put the fill it what ever view you want.
layout_header_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:id="@+id/id_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Irfan"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/id_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="Irfan55121@gmail.com"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<ImageView
android:id="@+id/id_profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:src="@mipmap/ic_profile_pic"
/>
</RelativeLayout>
then this header layout file will be in your activity_main.xml only
so in your MainActivity.java you can declare it as you do views from activity_main.xml and perform actions on it, no special code required.
do like this in your onCreate()
TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
tvUserName.setText("My Name");
tvUserName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
}
});
Hope it works Happy coding.