4

I got two similar buttons

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
    android:id="@+id/sync_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sync"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    />

</RelativeLayout>

added as items to menu

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/toggle_item"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/switch_button"
    />
<item
android:id="@+id/sync_item"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/sync"
/>
</menu>

I inflate items to menu in onCreateOptionsMenu with

getMenuInflater().inflate(R.menu.menu_main, menu);

Everything seems fine, but when i click in application, nothing happens as onOptionsItemSelected(MenuItem item) is never called

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Integer test = item.getItemId();
    switch (item.getItemId()) {
        case R.id.toggle_item:
          ...
        case R.id.sync_item:
          ...
  return super.onOptionsItemSelected(item);
 }

Adding toolbar with

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
user2925656
  • 383
  • 4
  • 17
  • 1
    Possible duplicate of [onOptionsItemSelected not called when using actionLayout (SherlockActionBar)](http://stackoverflow.com/questions/11627892/onoptionsitemselected-not-called-when-using-actionlayout-sherlockactionbar) – Daniel Nugent Feb 07 '16 at 20:11
  • I tried to add standard item next to it, this item fires event on click, my custom with defined actionLayout, it does not. – user2925656 Feb 08 '16 at 08:43

2 Answers2

7

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>
Community
  • 1
  • 1
user2925656
  • 383
  • 4
  • 17
  • 2
    Man, what the heck this is just as bad as the new material library 1.1.0+ they're trying to push onto us – lawonga Feb 18 '20 at 04:47
  • I found interesting to call on the `onClick` event to `boolean onOptionsItemSelected(@NonNull MenuItem item)` as it's pointed out here: https://stackoverflow.com/questions/26170423/onoptionsitemselected-not-getting-called-when-using-custom-action-view or even to `boolean onMenuItemSelected(int featureId, @NonNull MenuItem item)` if you want to propagate to fragments within your activity as it's mentioned on comments – marcRDZ Mar 16 '22 at 12:46
0

Accessing the menu on the toolbar item can work.

//kotlin
toolbar.menu.getItem(itemIndex).setOnMenuItemClickLister {}

You can do this for all the menu items

Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40