-2

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.

Community
  • 1
  • 1
David Ortiz
  • 997
  • 1
  • 14
  • 22
  • Aren't you missing the `switch(item.getId())` statement?? – Ali Bdeir Aug 05 '16 at 08:49
  • @AbAppletic Since I only have one item in the menu I don't really need to iterate over them, the loop in my code is a copy/paste of one of the solutions I found. However, it doesn't seem to be the problem – David Ortiz Aug 05 '16 at 08:51
  • Did you try putting the switch statement? I mean, I know it won't make sense but then we can solve this problem and then someone can answer the other created one – Ali Bdeir Aug 05 '16 at 08:53
  • I've updated the code with the switch – David Ortiz Aug 05 '16 at 08:58

1 Answers1

1

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

David Ortiz
  • 997
  • 1
  • 14
  • 22
  • I have removed it and changed the layout and the oncreateOptionsMenu and It does not solve the problem since the log does not appear – David Ortiz Aug 05 '16 at 09:19
  • 1
    @davidivad add - android:clickable="false" to com.joanzapata.iconify.widget.IconButton –  Aug 05 '16 at 09:28
  • Yes! android:clickable="false" fixed it. I guess there were several clickable items and android did not know which one was being clicked? Please add android:clickable="false" to your answer emphasizing that that solved the problem so that other fellow programmers can see it easily. Thank you very much. – David Ortiz Aug 05 '16 at 09:53