Skip to content Skip to sidebar Skip to footer

How To Listen To Long Click On A Navigationview Item?

I'm developing an android app using design library and appCompat library. I'm facing some issues when tried to listen to long click on a menu item. My app has a NavigationView as a

Solution 1:

Do it like this, but you have to set the popup_layout.xml into the MENU folder and write in the tags <menu> and not constraint layout or something like that.

in my Java class I wrote this for my list and important write R.menu.yourlayout):

listView = (ListView) findViewById(R.id.listView);

listView.setOnItemLongClickListener(newAdapterView.OnItemLongClickListener() {

    @OverridepublicbooleanonItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        PopupMenup=newPopupMenu(ViewListContents.this, v);
        MenuInflaterinflater= p.getMenuInflater();
        inflater.inflate(R.menu.popup_layout, p.getMenu());
        p.show();
        returntrue;
    }
});

If you have any questions ask me in the comments.

Solution 2:

update

You can use:

MenumyMenu= navigationView.getMenu();
MenuItemmyItem= myMenu.findItem(R.id.my_item);

myItem.setOnLongClickListener(newView.OnLongClickListener() {
    @OverridepublicbooleanonLongClick(View v) {
       // do something 

    }
});

Post a Comment for "How To Listen To Long Click On A Navigationview Item?"