I want to add a onClickListener to navigation view header's items like changing a TextView text or set an image for ImageView.
Asked
Active
Viewed 1.3k times
5
-
This question might benefit from some code e.g. show what you have tried so far. – Manos Nikolaidis Sep 20 '15 at 14:10
-
i haven't tried any code for this part,i want some thing like what gmail app do in navigation view header. – Amirhossein Validabadi Sep 20 '15 at 14:52
-
People won't give code like that. Try first yourself. – Andrei T Sep 20 '15 at 15:08
3 Answers
8
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...

Irfan
- 956
- 9
- 16
-
thanks i aleady added the navigation view,i just want handler for header,thannks any way – Amirhossein Validabadi Sep 20 '15 at 17:29
-
Beware that from support appcompat library v23.1 the way to get the headerView reference is to call `navigationView.getHeaderView(0)` (if you have only one header) – Marcel Bro Feb 03 '16 at 11:08
7
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();

Stephan Bauer
- 9,120
- 5
- 36
- 58

user6428609
- 101
- 1
- 1
1
Set onClickListener in XML on your view like so:
android:onClick="onLabelClick"
Then in your activity
public void onLabelClick(View view) {
// do stuff :)
}

Dixie Flatline
- 21
- 4