user634545 user634545 - 5 months ago 29
Android Question

onOptionsItemSelected not called when using actionLayout (SherlockActionBar)

The method onOptionsItemSelected isn't being called when using actionLayout in a menu item.
Am I missing something, or is it a known problem with SherlockActionBar?

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.article, menu);

super.onCreateOptionsMenu(menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

Log.d(TAG, "onOptionsItemSelected()");

switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.menu_item_comment:
return true;
default:
return super.onOptionsItemSelected(item);
}
}


Menu



<item android:id="@+id/menu_item_comment"
android:showAsAction="ifRoom"
android:actionLayout="@layout/action_bar_comment_layout"/>



Answer

well, you have to set onClickListener on that actionLayout to receive callback. I do it like this:

@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);
}