How to listen to long click on a navigationview item?
Asked Answered
K

2

5

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 side menu, inside this navigationview I had a menu with a lot of items. I can listen to click of these items but I can't listen to the long click. Does anybody know how do I implement it?

Thanks.

Klotz answered 30/9, 2015 at 12:21 Comment(0)
I
-3

update

You can use:

Menu myMenu = navigationView.getMenu();
MenuItem myItem = myMenu.findItem(R.id.my_item);

myItem.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
     public boolean onLongClick(View v) {
       // do something 

    }
});
Interfaith answered 30/9, 2015 at 12:41 Comment(4)
This will set onLongclickListener on my NavigationView, but I must implement it on a navigation view item.Klotz
@AndersonNunesdaSilva please take a look at my code now and see if it helps.Interfaith
For me the setOnLongClickListener(...) method doesn't exist. Any solutions? https://mcmap.net/q/736318/-how-to-set-a-long-click-listener-on-a-menuitem-on-a-navigationview/4583267Primogenial
@ThomasVos look to my answer at the bottomAdamek
A
0

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(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        PopupMenu p = new PopupMenu(ViewListContents.this, v);
        MenuInflater inflater = p.getMenuInflater();
        inflater.inflate(R.menu.popup_layout, p.getMenu());
        p.show();
        return true;
    }
});

If you have any questions ask me in the comments.

Adamek answered 8/10, 2020 at 6:3 Comment(0)
I
-3

update

You can use:

Menu myMenu = navigationView.getMenu();
MenuItem myItem = myMenu.findItem(R.id.my_item);

myItem.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
     public boolean onLongClick(View v) {
       // do something 

    }
});
Interfaith answered 30/9, 2015 at 12:41 Comment(4)
This will set onLongclickListener on my NavigationView, but I must implement it on a navigation view item.Klotz
@AndersonNunesdaSilva please take a look at my code now and see if it helps.Interfaith
For me the setOnLongClickListener(...) method doesn't exist. Any solutions? https://mcmap.net/q/736318/-how-to-set-a-long-click-listener-on-a-menuitem-on-a-navigationview/4583267Primogenial
@ThomasVos look to my answer at the bottomAdamek

© 2022 - 2024 — McMap. All rights reserved.