davidivad davidivad - 4 months ago 11
Android Question

onOptionsItemSelected listener not working

I'm developing an android app and I have an issue with the options menu. The onOptionsItemSelected() method is not called when the menu item is pressed, I'm using an actionLayout so, as many questions posted in this site, I've set my own listener. Nothing seems to work and I can't see how my solution differs from others.

Here it's my code:


On MainActivity.java


@Override
public boolean onCreateOptionsMenu(Menu menu) {

Log.d("Listener", "Created");
getMenuInflater().inflate(R.menu.main, menu);
for (int i = 0; i < menu.size(); i++) {
final MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.action_cart) {
View itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Listener", "Clicked1");
onOptionsItemSelected(item);
}
});
}
}
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_cart:
Log.d("Listener", "Clicked");
CartFragment newFragment = new CartFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}

}



On menu/main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="cart"
android:id="@+id/action_cart"
app:showAsAction="always"
app:actionLayout="@layout/badge_layout"/>

</menu>



On layout/badge_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">

<com.joanzapata.iconify.widget.IconButton
android:layout_width="44dp"
android:layout_height="44dp"
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="@drawable/badge_circle"/>
</FrameLayout>


As for the logs, "Listener: Created" is the only one printed.

I've already tried the solutions of several related questions like this one and this one. The actionlayout represents a badge inside a shopping cart and I know it has something to do with that since before the actionlayout it worked.

Any help is appreciated. Thanks.

Answer

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