onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
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);
}
onOptionsItemSelected in Menu is not clickable for item using actionLayout
override fun onCreateOptionsMenu(menu_: Menu): Boolean {
menu = menu_
menuInflater.inflate(R.menu.uppermenu, menu)
var cartMenuItem = menu!!.findItem(R.id.Cart)
cartMenuItem.actionView?.cart.setOnClickListener {
cartMenuItem.actionView?.cart_badge.text = "content update in the textview"
}
return true
}
The above snippet should perform onClick event for you
onOptionsItemSelected() not called when clicking on menu item which has an actionLayout set on it
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);
}
Android custom toolbar onOptionsItemSelected not working
I finally found the answer. Combination of these two answers solved my issue:
https://stackoverflow.com/a/17764895/2925656
https://stackoverflow.com/a/23936117/2925656
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.sync_button);
item.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
do_stuff;
}
});
return true;
}
Menu item:
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/toggle" />
Active layout must implement:
android:clickable="false"
My toggle action layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false">
<ToggleButton
android:id="@+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Discovery"
android:textOff="Favourite"
android:clickable="false"/>
</RelativeLayout>
onOptionsItemSelected listener not working
remove the onOptionsItemSelected method and change the oncreateOptionsMenu to
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_cart);
if (item != null) {
item.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Listener", "Clicked");
}
});
}
return true;
}
and the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">
<com.joanzapata.iconify.widget.IconButton
android:layout_width="44dp"
android:layout_height="44dp"
android:clickable="false"
android:textSize="24sp"
android:textColor="#ffffff"
android:background="@mipmap/ic_cart"
android:id="@+id/badge_icon_button"/>
<TextView
android:id="@+id/badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/badge_icon_button"
android:layout_alignLeft="@id/badge_icon_button"
android:layout_alignStart="@id/badge_icon_button"
android:text="10"
android:paddingEnd="12dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold"
android:background="@android:color/holo_red_dark"/>
</RelativeLayout>
What really solved the problem was android:clickable="false" on com.joanzapata.iconify.widget.IconButton
Monodroid ActionBarSherlock OnCreateOptionsMenu not called
I finally found the solution. Since I am inflating a view to be used as a menu item, I must take care myself to provide a handler when menu is click. Here is what I have done:
SupportMenuInflater.Inflate( Resource.Layout.menu_done, menu );
ActionBar_Sherlock.View.IMenuItem item = menu.FindItem( Resource.Id.action_menu_done );
LinearLayout layout = item.ActionView.FindViewById<LinearLayout>(Resource.Id.menu_container );
layout.Click +=new EventHandler(layout_Click);
Resource.Id.menu_container is a LinearLayout defined in my inflated view. It is clickable. So I provided a handler to its click event. This is the way I have solved my problem. Is there any other more elegant solution?
Android action bar menu item with actionLayout not working properly
Try this code in your activity.
Be sure to properly set your
R.menu.menuidentifier
R.id.menuitemidentifier
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarhelpmenu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m.performIdentifierAction(item.getItemId(), 0);
}
});
return true;
}
Hide menu items when ActionLayout is opened
This is what i ended up using.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// hide action item
if (menu != null) {
menu.findItem(R.id.notifications).setVisible(false);
menu.findItem(R.id.share).setVisible(false);
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
adapter.getFilter().filter("");
// re-show the action button
if (menu != null) {
menu.findItem(R.id.notifications).setVisible(true);
menu.findItem(R.id.share).setVisible(true);
}
return false;
}
});
return true;
}
private void setupSearchView(MenuItem searchItem) {
//code
}
Related Topics
Change Progressbar Color Through Code Only in Android
User Does Not Have Permission to Access This Object . Firebase Storage Android
Android How to Wait Until a Service Is Actually Connected
How to Programmatically Pair a Bluetooth Device on Android
Android: How to Check How Much Memory Is Remaining
How to Capture Key Events from Bluetooth Headset with Android
Just Upgraded My Os X to Yosemite, Android Device Chooser Not Showing Device - What to Do
Speechrecognizer Causes Anr... I Need Help with Android Speech API
How to Add Cardview in Layout Xml in Androidx
How to Use The Limit Argument in an Sqlite Query with Android
Is Discard Bad for Program Performance in Opengl
Remove Android Default Action Bar
How to Find Android Textview Number of Characters Per Line
How to Execute Background Task When Android App Is Closed/Set to Background