Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onOptionsItemSelected not called when using actionLayout (SherlockActionBar)

Tags:

android

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"/>

like image 805
user634545 Avatar asked Jul 24 '12 09:07

user634545


6 Answers

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);
}
like image 129
Tomislav Novoselec Avatar answered Nov 11 '22 09:11

Tomislav Novoselec


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.

like image 35
Luten Avatar answered Nov 11 '22 09:11

Luten


You should use MenuItemCompat.getActionView(menuItem); instead of item.getActionView(); if you are developing for older version.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        for (int i = 0; i< menu.size() ;i++) {
            MenuItem menuItem = menu.getItem(i);
            if (menuItem.getItemId() == R.id.add_item) {
                View view = MenuItemCompat.getActionView(menuItem);
                if (view != null) {
                    view.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(MainActivity.this, ToDoActivity.class);
                            startActivity(intent);
                        }
                    });
                }
            }
        }       
        return true;
    }
like image 10
Arun Kumar Avatar answered Nov 11 '22 11:11

Arun Kumar


 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_add_require, menu)

    val menuItem = menu!!.findItem(R.id.menu_cart)
    val view = menuItem.actionView
    view.setOnClickListener {
        onOptionsItemSelected(menuItem)
    }

    return true
}

Working for me (code is in kotlin)

like image 3
Tijo Thomas Avatar answered Nov 11 '22 10:11

Tijo Thomas


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    View view = menu.findItem(R.id.menu_item_comment).getActionView();
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
    });
    return true;
}

Also, (and that was very important for me, so other answers did not work) you need to disable the clickable option of all views in your action layout (that is, action_bar_comment_layout.xml):

android:clickable="false"
like image 1
Alexey Timokhin Avatar answered Nov 11 '22 09:11

Alexey Timokhin


Combining @Arun Kumar's and @Luten's answers, the below method will make the implementation generic. For all the menu items using actionView, we setOnClickListener to call onOptionsItemSelected(item). This way we can mix and match normal and actionLayout menu items, without worrying about setting individual onClickListeners.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(menuResourceId(), menu);

    for (int i = 0; i < menu.size(); i++) {
        final MenuItem item = menu.getItem(i);
        View actionView = MenuItemCompat.getActionView(item);
        if (actionView != null) {
            actionView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    onOptionsItemSelected(item);
                }
            });
        }
    }

    super.onCreateOptionsMenu(menu, inflater);
}
like image 1
anemo Avatar answered Nov 11 '22 11:11

anemo