1

I have a menu that lives in the action bar:

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

...

   <item android:id="@+id/map_types_item"
     android:title="@string/map_menu_map_type_text"
     asam:showAsAction="always"
     android:icon="@drawable/ic_action_bar_map"
     android:menuCategory="system"
     android:orderInCategory="300">

         <menu android:id="@+id/map_types_menu">
            <group android:checkableBehavior="single">
                <item android:id="@+id/map_type_normal"
                    android:title="@string/map_type_normal_text"
                    />

                <item android:id="@+id/map_type_satellite"
                    android:title="@string/map_type_satellite_text"
                    />

                <item android:id="@+id/map_type_hybrid"
                    android:title="@string/map_type_hybrid_text"
                    />
            </group>
        </menu>


     </item>
</menu>

And I would like to programitcally click that button such that the menu opens in the action bar.

Is that possible?

Tried:

mapTypeMenu = menu.findItem(R.id.map_types_item);

OR

mapTypeMenu = menu.findItem(R.id.map_types_menu);

Only R.id.map_types_item is found.

Then to programmatically open:

onOptionsItemSelected(mapTypeMenuItem);

Is this possible? If so what am I doing wrong?

EDIT:

This image shows my action bar. The map icon it the one I am programmatically trying to click:

enter image description here

This image shows what it looks like when clicked from the UI. I would like to open that programmatically as well:

enter image description here

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • How about you extract the map_types_item handling from your `onOptionsItemSelected` / `onMenuItemSelected` method into a separate method, and then call that method from the other function instead of doing this menu-finding workaround? – rekaszeru Dec 20 '14 at 22:29
  • I don't handle that top menu, Android does. When I click it in the UI (from action bar) it opens a menu underneath the action bar. I do not want to programmatically click the children items, I want to just programmatically open the menu underneath the action bar. – lostintranslation Dec 20 '14 at 22:31
  • No, I want to open the menu under one of my icons in the action bar. Not the default menu. – lostintranslation Dec 20 '14 at 22:37
  • I don't think that's (easily) possible, nor good UI practice, that menu is opened by the ActionBar object. I suggest you to create an additional dialog with those options, shouldn't take more than a couple lines. Then you can definitely call the menu option handler to execute the action – rupps Dec 21 '14 at 00:02

1 Answers1

3

Possible duplicate of this question.


Call menu.performIdentifierAction(R.id.map_types_item, 0);.

Community
  • 1
  • 1
Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
  • 1
    No, I want to open the menu under one of my icons in the action bar. Not the default menu. Check the xml I have an item (in the action bar) that has a menu in it. When clicked in UI it drops down the options. I would also like to be able to programmatically do that. – lostintranslation Dec 20 '14 at 22:37