OnClick for navigation drawer header not working

前端 未结 6 574
鱼传尺愫
鱼传尺愫 2020-12-05 07:25

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.

相关标签:
6条回答
  • 2020-12-05 07:50

    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
            }
        });
    
    0 讨论(0)
  • 2020-12-05 07:53

    Try like this

        navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // your code here.....
            }
        });
    
    0 讨论(0)
  • 2020-12-05 07:54

    For me other Answers didnt work. I have tried the below code. I know it's too late.

    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 
            }
        });
    
    0 讨论(0)
  • 2020-12-05 08:00

    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.

    0 讨论(0)
  • 2020-12-05 08:07

    just add this to your header layout Xml file

    android:focusable="true"
    android:focusableInTouchMode="true"
    android:clickable="true"
    
    0 讨论(0)
  • 2020-12-05 08:09

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

    0 讨论(0)
提交回复
热议问题