How to get MenuItem position in the listener using the new NavigationView
Asked Answered
N

6

12

The topic says it all. How should I go about retrieving the item position on the onClick listener using NavigationView? Also, why is there no getHeader method? Lastly I am doing everything programmatically, but the header is still clickable. Any thoughts?

Thanks!

Nigrosine answered 7/8, 2015 at 7:5 Comment(1)
You can set listener using navigationView.setNavigationItemSelectedListener() and implementing NavigationView.OnNavigationItemSelectedListener for receiving item clickLassiter
P
9

UPDATE

You can get position using this trick

final List<MenuItem> items = new ArrayList<>();
Menu menu;

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
menu = navigationView.getMenu();

for(int i = 0; i < menu.size(); i++){
    items.add(menu.getItem(i));
}

navigationView.setNavigationItemSelectedListener(new  NavigationView.OnNavigationItemSelectedListener(){
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        // update highlighted item in the navigation menu
        menuItem.setChecked(true);
        int position = items.indexOf(menuItem);

        return true;
    }
});
Periclean answered 7/8, 2015 at 7:36 Comment(3)
Thanks, I am familiar with the listener, but I don't understand why they don't implement a getPosition() method. What is the intended use case?Nigrosine
If menuitem obj is there you can pass that as parameter to any method to perform anything...Periclean
Could you be more specific? ThanksNigrosine
E
11

I found a simple solution. You can assign an order using Menu's add(...) method. Then you can retrieve the order using MenuItems's getOrder(...) method. If you are using xml, you can use android:orderInCategory="...".

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
Menu menu = navigationView.getMenu();

for(int i=0; i < menu.size(); i++){
    items.add(Menu.NONE, Menu.NONE, i, menu.getItem(i));
}

navigationView.setNavigationItemSelectedListener(new  NavigationView.OnNavigationItemSelectedListener(){
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        // update highlighted item in the navigation menu
        menuItem.setChecked(true);
        int position=items.getOrder();
        return true;
    }
});
Entoderm answered 23/2, 2016 at 7:20 Comment(3)
Don't know if this is the correct way to do it, but it worked, so thanks (Y)Facture
I consider the best option which you say about switch (item.getOrder()) and put android:orderInCategory="..." in the XML. Thank you very much.Spit
This seems to be the best way to do this. Thanks!Esprit
P
9

UPDATE

You can get position using this trick

final List<MenuItem> items = new ArrayList<>();
Menu menu;

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
menu = navigationView.getMenu();

for(int i = 0; i < menu.size(); i++){
    items.add(menu.getItem(i));
}

navigationView.setNavigationItemSelectedListener(new  NavigationView.OnNavigationItemSelectedListener(){
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        // update highlighted item in the navigation menu
        menuItem.setChecked(true);
        int position = items.indexOf(menuItem);

        return true;
    }
});
Periclean answered 7/8, 2015 at 7:36 Comment(3)
Thanks, I am familiar with the listener, but I don't understand why they don't implement a getPosition() method. What is the intended use case?Nigrosine
If menuitem obj is there you can pass that as parameter to any method to perform anything...Periclean
Could you be more specific? ThanksNigrosine
I
5

You can just take its order if you specify the "android:orderInCategory" attribute for menu items:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:orderInCategory="0"
        android:title="@string/news" />
    <item
        android:orderInCategory="1"
        android:title="@string/search" />
</menu>
val navigationView = findViewById<NavigationView>(R.id.navigation)

navigationView.setNavigationItemSelectedListener { menuItem ->
    val menuItemOrder = menuItem.order

    true
}

Or, use this in case you don't want to specify orders by hand:

val navigationView = findViewById<NavigationView>(R.id.navigation)

navigationView.setNavigationItemSelectedListener { menuItem ->
    val menuItemIndex = bottomNavigation.menu.children.indexOf(menuItem)

    true
}
Isomerize answered 16/11, 2020 at 8:16 Comment(1)
This answer is perfect!Stabilize
P
1

If you are using menu_drawer.xml, you just have to add an id in the items like this:

<item
    android:id="@+id/nav_top_stories"
    android:title="@string/txt.menu.item1"
/>

With this you just have to test on menuItm.getId():

 navigationView.setNavigationItemSelectedListener(new  NavigationView.OnNavigationItemSelectedListener(){
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        // update highlighted item in the navigation menu
        menuItem.setChecked(true);

        switch(menuItem.getId()){
           case R.id.txt_menu_item1 : //do what you want to do;
           break;
           case R.id.txt_menu_item2 : // etc,
        }
        return true;
    }
});

If you are using dynamic menu, just use this method to add an item to you navigation drawer:

NavigationView.getMenu().add(int groupId, int itemId, int order, CharSequence title)

And then test by the order:

navigationView.setNavigationItemSelectedListener(new  NavigationView.OnNavigationItemSelectedListener(){
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        // update highlighted item in the navigation menu
        menuItem.setChecked(true);

        switch(menuItem.getOrder()){
           case 0 : //do what you want to do;
           break;
           case 1 : // etc,
           default : //do whatever you want ;
        }
       return true;
    }
});
Possing answered 26/2, 2017 at 0:34 Comment(0)
I
0

In my case, i use

first test whit this..

Log.d(TAG, navigationView.getMenu().getItem(0).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(1).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(2).isChecked());

next this...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        if(navigationView.getMenu().getItem(0).isChecked()) {
            cerrarSesion();
        }else {
            navigationView.getMenu().getItem(0).setChecked(true);
            seleccionarItem(navigationView.getMenu().getItem(0));
        }
    }
    return false;
}
Idyll answered 11/7, 2016 at 14:41 Comment(0)
G
0

The easy, and most clean (in my opinion) way:

private int getBottomNavigationItemPosition(@MenuRes int itemId) {
    final Menu bottomNavigationMenu = mBottomNavigationView.getMenu();
    final int menuItemCount = bottomNavigationMenu.size();
    for (int i = 0; i < menuItemCount; i++) {
        if (bottomNavigationMenu.getItem(i).getItemId() == itemId) return i;
    }
    return -1; // the item with itemId was not found in Menu
}
Gouveia answered 29/7, 2022 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.