6

In my action bar, I have defined a menu item that can show text "DONE" by the code below:

Menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_register_text"
    android:actionLayout="@layout/action_done_text"
    android:title="@string/action_done"
    android:showAsAction="always"/>

</menu>

action_done_text.xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/expand_activities_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">

<TextView
    android:id="@+id/register_action_bar_done"
    android:layout_width="53dp"
    android:layout_height="35dp"
    android:layout_gravity="center"
    android:layout_marginRight="10dip"
    android:gravity="center"
    android:text="DONE" />

</FrameLayout>

I have onCreateOptionsMenu implement properly in the code, and the view can show the text correctly, but just when I tap on the DONE text, onOptionsItemSelected is not called. To me, it seems like the click event is not recognized.

I was wondering if the above way is not a good way to add a text menu item?

user
  • 86,916
  • 18
  • 197
  • 190
Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
  • 1
    Please don't prefix your questions titles with `Android`, the tag at the bottom is more than enough, you'd just be adding clutter to the title. It's normal that the callback isn't triggered as you could have different views in the action layout and Android can't guess which one should trigger the event. In the `onCreateOptionMenu()` get a reference to the menu item, retrieve its action view and set a click listener on it. – user Jan 05 '14 at 09:25

1 Answers1

5

Use this as shown in onOptionsItemSelected not called when using actionLayout (SherlockActionBar)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.map_menu, menu);
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == R.id.menu_more) {
            itemChooser = item.getActionView();
            if (itemChooser != null) {
                itemChooser.setOnClickListener(this);
            }
        }
    }
    return super.onCreateOptionsMenu(menu);
}
Community
  • 1
  • 1
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33