Skip to content Skip to sidebar Skip to footer

Onoptionsitemselected Not Being Called For Action Bar Menu Item With Custom Actionlayout

I'm trying to implement a notification icon in my actionbar to show the count of notifications. Something like I've added a custom layout file for it NotificationIcon.xml: <

Solution 1:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        MenuInflaterinflater= getSupportMenuInflater();

                inflater.inflate(R.menu.main_activity_actions, menu);

        final View notification_icon= menu.findItem(R.id.notification_icon).getActionView();


        notification_icon.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View v) {
// write ur code here
}

        });
        returnsuper.onCreateOptionsMenu(menu);
    }

use this code......hope it helps :)

Solution 2:

i struggle with the same Problem and found following Answer :

First of all: i really don't know why but if you delete the

android:clickable="true"

then ... and ONLY then you get the click event!!!

Second: you have to set the Listener...

...    
item.getActionView().setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            //Do Your Stuff.....
        }
});

May someone can Explain why this is with the "android:clickable" Thing... i tough the standard value IS true??

Solution 3:

You had made ImageButton clickable, and then written the clickListener for the Layout of menuItem. You can observe that if you click outside the ImageButton but inside the MenuItem layout, your listener will work. The reason is simple clickListener for ImageButton has nothing to do, on setting Clickable = true defines whether this button reacts to click events. Just simply set Clickable = false, your button will not react to its click event, eventually your whole layout will react to your defined clickListener and then your problem will be solved.

Post a Comment for "Onoptionsitemselected Not Being Called For Action Bar Menu Item With Custom Actionlayout"