OnClick for navigation drawer header not working
Solution 1
For me other Answers didn't work. I have tried the below code. I know it's too late. Hope this will help some.
What I did to access the view of header.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerview = navigationView.getHeaderView(0);
TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
profilename.setText("your name")
for clicking the views of header, here I have used a linearlayout of headerview
LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.START);
}
});
Or
headerview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Your code here
}
});
Solution 2
Don't forget to define android:clickable="true"
in your TextView xml.
Solution 3
Try like this
navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code here.....
}
});
Solution 4
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="[email protected]"
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.
Related videos on Youtube
Comments
-
varunkr about 3 years
I have a navigation drawer in my app which contains a header and some list items. The header has a textview which i want to make clickable but I am not able to do it.
To get the id of this textview I used the following code, since it is in a different layout file compared to the one in the setContentView in onCreate.
final LayoutInflater factory = getLayoutInflater(); final View textEntryView = factory.inflate(R.layout.header, null); TextView home = (TextView) textEntryView.findViewById(R.id.home); home.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show(); } });
header.xml contains the header of the navigation drawer. It has an item named home. I need to make it clickable. The above code is in the onCreate method.
-
Robin Davies almost 9 yearsYou're not placing the header in a NavigationView by any chance, are you? NavigtionView internally places the header in what ends up being slot 0 in a nested ListView view and then intercepts and discards the OnItemClicked event for the header. Could that be the source of the problem? Not quite sure of the mechanism at play, but IF the header is in a NavigationView that would important info.
-
varunkr almost 9 yearsOn looking carefully I found that I did just what you said. Is there a way to make it work in this case ? Thanks !!
-
Robin Davies almost 9 yearsProbably best to put the header outside the navigation view then. Or just not use NavigationView at all (since it generally seems to cause more problems than it solves). It's basically a fancy broken listview with adapter code for menus (which is not that great a feature).
-
-
ZeWolfe15 over 8 yearsWorked like a charm :)
-
Kishore about 8 yearsWorked for me too.!! Thanks alot :)
-
varunkr about 8 yearsSince this is working for everybody I will accept it.
-
Subin Babu over 6 yearsThanks, Sajini ! ,You are awesome
-
Anwar Zahid almost 3 yearsWawa, worked like a charm. But I add some add for community to get the specific resource on navigation header then use val backArrow = navDrawerID.getHeaderView(0).backDrawerArrowID backArrow.setOnClickListener(this)
-
Jenny almost 2 yearsi'm using this code show this error :-
on a null object reference