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"/>
Solution 1:
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);
}
Solution 2:
You'll have to add your own OnClickListener
and explicitly call onOptionsItemSelected
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem awesomeMenuItem = menu.findItem(R.id.action_awesome);
View awesomeActionView = menuItem.getActionView();
awesomeActionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(awesomeMenuItem));
}
});
}
P.S: Don't know why it doesn't work out of the box.