Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onOptionsItemSelected in Menu is not clickable for item using actionLayout

I have an android app with options menu, and the first item is defined with "actionLayout":

menu.xml snippet:

    <item
    android:id="@+id/Cart"
    android:title="Cart"
    app:actionLayout="@layout/custom_action_item_layout"
    app:showAsAction="always"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings_white_24dp"
    android:title="@string/settings"
    app:showAsAction="ifRoom" />

custom_action_item_layout snippet:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true"
android:clickable="true">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/ic_shopping_cart_white_24dp"/>

<TextView
    android:id="@+id/cart_badge"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="right|end|top"
    android:layout_marginEnd="-5dp"
    android:layout_marginRight="-5dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/badge_background"
    android:gravity="center"
    android:padding="1dp"
    android:textColor="@android:color/white"

    android:textSize="12sp"/>

</FrameLayout>

then I set the menu up:

override fun onCreateOptionsMenu(menu_: Menu): Boolean {
    menu = menu_
    val inflater = menuInflater
    inflater.inflate(R.menu.uppermenu, menu)

    var cart = menu!!.findItem(R.id.Cart);
    var actionView = getActionView(cart);

    textCartItemCount = actionView.findViewById(R.id.cart_badge) as TextView

    return true

}

however, the

override fun onOptionsItemSelected(item: MenuItem): Boolean { }

is never called when I click on the menu item.

I looked up the solution for the sherlockactionbar from 2012 for java, but the solution does not apply well: onOptionsItemSelected not called when using actionLayout (SherlockActionBar)

It seems related to the action Layout of the item, Without this, it is working well. how to make the onOptionsItemSelected working?

like image 926
user1616685 Avatar asked Oct 22 '25 05:10

user1616685


1 Answers

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

like image 119
Raghul Vaikundam Avatar answered Oct 23 '25 21:10

Raghul Vaikundam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!