0

I followed the Android Developer documentation to create a navigation drawer.

Then I wanted to add 2 groups of menu items. One for user created lists, the other for labels. Since I couldn't directly set titles to groups, I followed the tutorial HERE and wrapped groups in tags.

My XML menu looks like this: XML Menu

In onCreate I find the NavigationView using its id, then get its menu, get the first by its id, get the item's SubMenu and add MenuItems:

NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu =  navigationView.getMenu();
MenuItem item = menu.findItem(R.id.item_lists);
SubMenu sbLists = item.getSubMenu();
sbLists.add(0, 0, 0, "Android").setIcon(R.drawable.ic_android);
sbLists.add(0, 1, 0, "iOS").setIcon(R.drawable.ic_ios);

And:

navigationView.setNavigationItemSelectedListener(
    menuItem -> {
        // set item as selected to persist highlight
        menuItem.setChecked(true);
        // close drawer when item is tapped
        mDrawerLayout.closeDrawers();

When I click the items added in XML, the drawer closes and if I open it again I can see the item is selected. However, with the items added in onCreate, this is what I see after I re-open the drawer:

Clicking this one Before click

It's gone After

I've tried searching on Stack Overflow and found only one question with the same problem Navigation item's title disappeared when clicked , however, the solution is to set the item text color to black. That doesn't fix the item not being selected problem and doesn't fix the icon disappearing either.

Please let me know what I'm doing incorrectly, thank you!

Edit: Here's what my Theme looks like in styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="iconColor">@color/icons_light</item>
    <item name="toolbarIconColor">@color/icons_light</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
</style>
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Akres
  • 126
  • 2
  • 16

2 Answers2

1

Solved the problem.

Even though I had declared android:checkableBehavior="single"> in XML for the group I was adding items to, I still had to manually do setCheckable(true) for each item added programatically.

subMenu.add(R.id.group_lists, ITEM_ID, ITEM_ORDER, "item").setIcon(R.drawable.ic_list).setCheckable(true);

Now, when I click an item created in either XML or Java, both the icon and the text are visible, the item is selected.

Akres
  • 126
  • 2
  • 16
0

I think you need to change the theme of your NavigationView set the colorPrimary and for the text and colorControlHighlight for the selected background element :

<style name="ThemeOverlay.AppCompat.myTheme">
    <item name="colorPrimary">@color/color-primary</item>
    <item name="colorControlHighlight">@color/color-primary</item>
</style>

and set it in your NavigationView :

<android.support.design.widget.NavigationView
        ...
        app:theme="@style/ThemeOverlay.AppCompat.myTheme">
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • The XML items, when selected, lose their icon and text and are filled by the colorPrimary. And I still cannot select items added in Java: Here's a video of how that looks: https://youtu.be/m0ODuheNik8 – Akres Dec 23 '18 at 13:09
  • it's because the theme you are using – Oussema Aroua Dec 23 '18 at 13:11
  • Okay, I've added the theme at the bottom of my question. What should I modify to get this up and running? – Akres Dec 23 '18 at 13:15
  • I replied because I tried your solution. I also provided a video showing the effects of your solution. Answer - it did not work, unfortunately. – Akres Dec 23 '18 at 15:23