27

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.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • You'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. – Robin Davies Jul 30 '15 at 06:03
  • On looking carefully I found that I did just what you said. Is there a way to make it work in this case ? Thanks !! – varunkr Jul 30 '15 at 10:11
  • Probably 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). – Robin Davies Aug 02 '15 at 16:19

6 Answers6

118

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 
        }
    });
SimplyProgrammer
  • 1,799
  • 2
  • 17
  • 28
1

Don't forget to define android:clickable="true" in your TextView xml.

frgnvola
  • 518
  • 3
  • 16
1

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.

Irfan
  • 956
  • 9
  • 16
1

Try like this

    navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // your code here.....
        }
    });
0

just add this to your header layout Xml file

android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
0

First fetch header view

 View headerView = navigationView.getHeaderView(0);

And then use onClick Listener

  headerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // code
        }
    });
ANUJ GUPTA
  • 905
  • 13
  • 22