Home > Enterprise >  Android Navigation Rail Headerview onclick not working
Android Navigation Rail Headerview onclick not working

Time:08-17

I'm trying to set onclicklistener on navigation railview but it doesn't work for me

Java code

 binding.navigationRail.getHeaderView().setClickable(true);
    binding.navigationRail.getHeaderView().setOnClickListener(v -> {
        Toast.makeText(this, "headerview", Toast.LENGTH_SHORT).show();
    });

Xml

    <com.google.android.material.navigationrail.NavigationRailView
    android:id="@ id/navigation_rail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:headerLayout="@layout/floatbutton_add_product"
    app:menu="@menu/navigation_rail_menu" />

CodePudding user response:

that's worked for me

        View header=binding.navigationRail.getHeaderView();
    FloatingActionButton FloatingActionButton=  header.findViewById(R.id.nav_rail_fab);
    FloatingActionButton.setOnClickListener(v -> {
        Toast.makeText(MainActivity.this, "headerview", Toast.LENGTH_SHORT).show();
    });
  • Related