V. Govorov V. Govorov - 5 months ago 15
Android Question

Android - Find id of item in SubMenu

I use Navigation activities for moving from one Activity to another. And I want to add submenu to menu of NavigationView< but can't find way to find id og item in submenu.

<menu>
<item android:title="Communicate">
<menu>
<item android:id="@+id/about_us" android:icon="@android:drawable/ic_menu_share"
android:title="About us" />
</menu>
</item></menu>


 

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

switch (id) {
case R.id.nav_order: {
break;
}
case R.id.nav_search: {
break;
}
}

return true;
}


I want to add:
case R.id.about_us
.
But Android Studio can't find this id.
How can i make it and what i do wrong?

Answer

Your code worked fine for. There is one thing missing in your menu.that is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"/> <!--the xmlns is missing -->
</menu>