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!

Fhl
  • 1,079
  • 1
  • 12
  • 26
  • You can set listener using `navigationView.setNavigationItemSelectedListener()` and implementing `NavigationView.OnNavigationItemSelectedListener` for receiving item click – Jaiprakash Soni Aug 07 '15 at 07:11

6 Answers6

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;
    }
});
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
Rami
  • 146
  • 2
  • 12
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;
    }
});
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sagar Devkota
  • 1,192
  • 8
  • 13
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
}
Igor Kharakhordin
  • 9,185
  • 3
  • 40
  • 39
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;
    }
});
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Anis MARZOUK
  • 266
  • 2
  • 9
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;
}
Maguz
  • 132
  • 6
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
}
BamsBamx
  • 4,139
  • 4
  • 38
  • 63