1

I have 3 menus in my navigation view where each menu have few more items. Im having problems when i press each item in menu, i want checked item to be selected but other items non selected. Bellow is picture so that you can see what is the problem:

For selection im using item.setChecked(true); in onNavigationItemSelected(MenuItem menu). What i want is only 1 item to be selected, so when i checked one item, i want that other items are not selected.

I search on google but usually they say that i need to separate each menu into group android:checkableBehavior="single" what i done but, it still do not work:

EDIT START:

I just find solution, instead of item.setChecked(true) i write navigationView.setCheckedItem(R.id.itemId); and everything works now.

I find solution on this link:

EDIT END

enter image description here

Here is the code for drawer_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="Animals">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/dog"
                    android:icon="@drawable/ic_action_dog"
                    android:title="Domestic Animals" />

                <item
                    android:id="@+id/wild"
                    android:icon="@drawable/ic_action_dog"
                    android:title="Wild Animals" />

                <item
                    android:id="@+id/bird"
                    android:icon="@drawable/ic_action_bird"
                    android:title="Birds" />

                <item
                    android:id="@+id/fish"
                    android:icon="@drawable/ic_action_fish"
                    android:title="Sea Animals" />

                <item
                    android:id="@+id/insects"
                    android:icon="@drawable/ic_action_bubamara"
                    android:title="Insects" />
            </group>
        </menu>
    </item>

    <item android:title="Others">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/car"
                    android:icon="@drawable/ic_action_car"
                    android:title="Cars" />


                <item
                    android:id="@+id/smiley"
                    android:icon="@drawable/ic_action_smiley"
                    android:title="Laugh" />

                <item
                    android:id="@+id/earth"
                    android:icon="@drawable/ic_action_earth"
                    android:title="Nature" />

                <item
                    android:id="@+id/sounds"
                    android:icon="@drawable/ic_action_zvucnik"
                    android:title="Effects" />

                <item
                    android:id="@+id/tools"
                    android:icon="@drawable/ic_action_clock"
                    android:title="Others" />

            </group>
        </menu>
    </item>

    <item android:title="Change pictures">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/change_picture_id"
                    android:icon="@drawable/ic_action_promjena_slike"
                    android:title="Change Picture"></item>
            </group>
        </menu>
    </item>

</menu>

Here is the code for navigation drawer menu item click events in main activity:

     navigationView = (NavigationView) findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.dog:
                        homeFragment.sortiranje("Domestic", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.wild:
                        homeFragment.sortiranje("Wild", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.bird:

                        homeFragment.sortiranje("Birds", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.fish:
                        homeFragment.sortiranje("Sea", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.insects:
                        homeFragment.sortiranje("Insects", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.car:
                        homeFragment.sortiranje("Cars", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.smiley:
                        homeFragment.sortiranje("Laugh", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.earth:
                        homeFragment.sortiranje("Nature", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.sounds:
                        homeFragment.sortiranje("Effects", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.tools:
                        homeFragment.sortiranje("Others", tagonja);
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.change_picture_id:
                        item.setChecked(true);
                        if (tagonja == 0) {
                            tagonja = 1;
                            homeFragment.promjenaSlike(tagonja);
                        } else if (tagonja == 1) {
                            tagonja = 0;
                            homeFragment.promjenaSlike(tagonja);
                        }

                        drawerLayout.closeDrawers();

                        break;
                }

                return false;
            }
        });
Community
  • 1
  • 1
NoName
  • 273
  • 7
  • 21
  • Try adding unique id's to all groups like: ``, and this way for each of the groups. – Vucko May 14 '16 at 12:02
  • I added for each group new id, everything is the same. Should i change code for menu item click in main activity? – NoName May 14 '16 at 12:11
  • Hmm.. Looking at my code (I have 2 groups and it's always 1 item selected), you need to remove `item.setChecked(true);` because Android studio takes care of that itself. Remove that. And also I dont even have 'closeDrawer' there, it does that automatically also. Where are you from when you're calling the methods 'sortiranje'? – Vucko May 14 '16 at 12:14
  • With method method sortiranje() i just populate fragment with data. Depending on what menu item is selected i will have different data for that menu item. I remove item.setCheded(true) and now when i click menu item it is not selected. Also, when i remove closeDrawer, drawer do not close automatic. – NoName May 14 '16 at 12:19
  • I know, I know, I just asked because you're probably from Bosnia or Serbia, cause I'm from Serbia as well :) Did you try removing setChecked? – Vucko May 14 '16 at 12:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111927/discussion-between-vucko-and-beginner). – Vucko May 14 '16 at 12:57
  • 1
    I just find solution. I just need to replace method item.setChecked(true) with navigationView.setCheckedItem(R.id.itemID); – NoName May 14 '16 at 13:43

0 Answers0