1

I am trying to check a default item in a menu group. I am not changing anything in the menu using code.

The menu is as follows:

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

<item android:title="Map type">
    <menu>
        <group
            android:checkableBehavior="single"
            >
            <item
                android:id="@+id/nav_map_type_normal"
                android:title="Normal"
                android:checked="true"
                />
            <item
                android:id="@+id/nav_map_type_satellite"
                android:title="Satellite"
                />
            <item
                android:id="@+id/nav_map_type_terrain"
                android:title="Terrain"
                />
            <item
                android:id="@+id/nav_map_type_hybrid"
                android:title="Hybrid"
                />
        </group>
    </menu>
</item>

<item
    android:id="@+id/nav_toggle_traffic"
    android:title="Toggle traffic"
    />

<item
    android:id="@+id/nav_settings"
    android:icon="@drawable/ic_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    />

</menu>

When the menu is first displayed, the first item (Normal) is highlighted to indicate it is checked. This is what I want to happen. I then want the highlighting to move through the group as each item is pressed.

The problem is:

  • If I press Satellite, BOTH Normal and Satellite are highlighted

    If I then press Hybrid, Normal remains highlighted, Hybrid is highlighted and Satellite reverts to its unchecked state.

Only after actually pressing Normal does its checked state behave as it should.

I have tried removing the android:checked="true" and using performIdentifierAction in code, but this did not change the checked state.

Chris Read
  • 307
  • 4
  • 11

2 Answers2

0

Since you are making NORMAL as default TRUE, you need explicitly SET it to false as and when Satellite, Hybrid or Terrain is selected.

I believe currently, you would be setting NORMAL as android:checked=false on the onclickmenu selection code of nav_map_type_normal. i.e you are basically un-checking it as by default it is selected as TRUE. Please do the same when other options are selected.

Natarajan Raman
  • 606
  • 1
  • 4
  • 14
  • I can get it to appear to be working by just setting the Normal option as checked(false) whenever another option is pressed, but I really want it to work without the need to add code. I think it **should** work as if it is a group of radio buttons, in which case one button would always be selected. – Chris Read Apr 06 '16 at 04:44
  • Yes, if you want to make it work without adding the code across, the you should use RadioGroup and RadioButtons as mentioned here - http://developer.android.com/guide/topics/ui/controls/radiobutton.html – Natarajan Raman Apr 06 '16 at 04:56
0

Sorry, I forgot to mention that the menu is used in a NavigationView and as it it turns out that changes everything.

navigationView.setCheckedItem(R.id.nav_map_type_normal);

performs exactly the way I want. I found it in a comment here Navigation drawer: How do I set the selected item at startup?

Community
  • 1
  • 1
Chris Read
  • 307
  • 4
  • 11