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
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"):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/nav_camara" android:icon="@android:drawable/ic_menu_camera"
android:title="Import" />
<item android:id="@+id/nav_gallery" android:icon="@android:drawable/ic_menu_gallery"
android:title="Gallery" />
<item android:id="@+id/nav_slideshow" android:icon="@android:drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item android:id="@+id/nav_manage" android:icon="@android:drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/nav_share" android:icon="@android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="@+id/nav_send" android:icon="@android:drawable/ic_menu_send"
android:title="Send" />
</group>
</menu>
</item>
<item android:title="BNK">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/nav_share_bnk" android:icon="@android:drawable/ic_menu_share"
android:title="Share BNK" />
<item android:id="@+id/nav_send_bnk" android:icon="@android:drawable/ic_menu_send"
android:title="Send BNK" />
</group>
</menu>
</item>
</menu>
And here is the screenshot
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!