{"id":181,"date":"2016-07-05T22:33:22","date_gmt":"2016-07-05T22:33:22","guid":{"rendered":"https:\/\/wp.local\/?p=181"},"modified":"2016-07-05T22:34:14","modified_gmt":"2016-07-05T22:34:14","slug":"navigationdrawer-navigationview-check-menu-subitems","status":"publish","type":"post","link":"https:\/\/blog.c-devtech.net\/?p=181","title":{"rendered":"NavigationDrawer \/ NavigationView check menu subitems"},"content":{"rendered":"<p>I needed a\u00a0 <em>NavigationDrawer<\/em> in an app I was building and decided to try the &#8220;new&#8221; Material Design Support Library and use a <em>NavigationView<\/em> to implement it. Using <em>Android Studio version 2.1,<\/em> I started the app by selecting the <strong>Navigation View Activity<\/strong>.<\/p>\n<p>Choosing the <em>Navigation View Activity<\/em> creates a project with a number of default files related to the <em>NavigationView<\/em> :<\/p>\n<ul>\n<li><em>activity_main.xml<\/em> which defines the <em>DrawerLayout<\/em> and its <em>NavigationView<\/em><\/li>\n<li><em>nav_header_main.xml<\/em> which defines the <em>NavigationView<\/em> header<\/li>\n<li><em>activity_main_drawer.xml<\/em> which defines the <em>NavigationView<\/em> menu<\/li>\n<\/ul>\n<p>and produces an example <em>NavigationView<\/em> that has a parent menu with four items and a submenu with two items. The main menu is set to automatically persist the selection by using<\/p>\n<pre>&lt;group android:checkableBehavior=\"single\"&gt;<\/pre>\n<p>With the submenu items acting as &#8220;action&#8221; selections that do not retain their state since the <em>CheckableBehaviour <\/em>was not set for the submenu.<\/p>\n<p>With my application, I have a main menu with a one static submenu defined in the xml file and then a second submenu generated dynamically in code. Even though there are two submenus, I still want only one item in the <em>NavigationView<\/em> selected and highlighted at a time. Since the\u00a0<em>CheckableBehaviour <\/em>does not extend across <em>&lt;menu&gt;<\/em> settings, I had to manually force the the highlight.<\/p>\n<p>Each item needed to have checkable set to true.\u00a0 Either in the xml:<\/p>\n<pre>android:checkable=\"true\"<\/pre>\n<p>or in code:<\/p>\n<pre>.setCheckable(true)<\/pre>\n<p>Then after managing the new item selected in<\/p>\n<pre>@Override\r\npublic boolean onNavigationItemSelected(MenuItem menuItem) {\r\n    int menuId = menuItem.getItemId();<\/pre>\n<p>I initially added the following which, unfortunately, did not work&#8230;<\/p>\n<pre>menuItem.setChecked(true);<\/pre>\n<p>but then I tried the following which did work to highlight the selected menu item in the\u00a0<em>NavigationView<\/em>:<\/p>\n<pre>\/\/ Highlight the selected item in the NavigationView\r\nNavigationView navigationView = \r\n        (NavigationView) findViewById(R.id.nav_view);\r\nnavigationView.setCheckedItem(menuId);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed a\u00a0 NavigationDrawer in an app I was building and decided to try the &#8220;new&#8221; Material Design Support Library and use a NavigationView to implement it. Using Android Studio version 2.1, I started the app by selecting the Navigation &hellip; <a href=\"https:\/\/blog.c-devtech.net\/?p=181\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-android-app-develoment"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1IEK3-2V","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181"}],"version-history":[{"count":3,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}