I have a working Navigation Drawer and having some issues with menuItem.setChecked(true);
when using groups and headers within the menu. It's not highlighting menu items as expected.
Here is my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityMap">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_welcome"
android:icon="@drawable/abc_btn_check_to_on_mtrl_000"
android:title="Welcome" />
<item
android:id="@+id/nav_map_showmap"
android:icon="@mipmap/ic_map_black_24dp"
android:title="Show Map" />
<item
android:icon="@mipmap/ic_list_black_24dp"
android:title="Show List" />
<item android:title="Settings">
<menu>
<item
android:id="@+id/nav_database_check"
android:icon="@mipmap/ic_cloud_done_black_24dp"
android:title="Update Database" />
<item
android:id="@+id/nav_map_settings"
android:icon="@mipmap/ic_settings_black_24dp"
android:title="App Preferences" />
</menu>
</item>
<item android:title="General">
<menu>
<item
android:id="@+id/nav_general_about"
android:icon="@mipmap/ic_info_black_24dp"
android:title="About" />
<item
android:id="@+id/nav_general_help"
android:icon="@mipmap/ic_help_black_24dp"
android:title="Help" />
<item
android:id="@+id/nav_general_report"
android:icon="@mipmap/ic_email_black_24dp"
android:title="Feedback / Report error" />
</menu>
</item>
</group>
</menu>
As you can see I have 3 menu items, then a Settings Group with 2 menu items, then a General Group with 3 menu items.
Now with the first 3 menu items, the menuItem.setChecked(true);
is working as expected, and highlighting that menu item. However, none of the following menu items within the subset ("Settings" or "General" subsets) are highlighting correctly.
I read that the <group android:checkableBehaviour="single">
can encapsulate the whole block, but that doesn't seem to be working.
Any thoughts? Cheers.
EDIT - Added Screenshot of the menu structure
Here you can use three different groups top , center and bottom. And first top group will have 3 menu items as you have . Then the center group can be same as your "Settings" menu with 2 items. And bottom group will have 2 items as it is in General.
And in your java file you can handle Item Selected Listener to handle the item click of the navigation menu.
Change the position on closing group tag
</group>
in your code as under:Look the position carefully and do it.
it work for me :D
UPDATE
If you have upgraded your Android Studio to 1.4, you can create a new project (with
Navigation Drawer Activity
template). Then, you can update your menu file as the following (pay attention to the two lines<group android:checkableBehavior="single">
inside 2 sub-menus at "Communicate" and "BNK"):And here is the screenshot
END OF UPDATE
As I have commented, please try putting
<group android:checkableBehavior="single">
below menus of Settings and General instead of putting below the outside menu. That means you will have 2 groups. Hope this helps!Try this: