4

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?

user1616685
  • 1,310
  • 1
  • 15
  • 36
  • You have to add onClickListener for your menu item? Just let me know what do you want to perform when the user clicks the menu item – Raghul Vaikundam Feb 20 '20 at 15:36
  • I call a method and update the textview that is inside the custom_action_item_layout it is similiar to this: https://stackoverflow.com/questions/30617771/onoptionsitemselected-not-being-called-for-action-bar-menu-item-with-custom-acti?rq=1 – user1616685 Feb 20 '20 at 15:39
  • I have updated the answer for you and just check and let me know if it works for you – Raghul Vaikundam Feb 20 '20 at 15:45
  • 1
    I still would like to know why the method is not called. – user1616685 Feb 20 '20 at 15:48

1 Answers1

4
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

Raghul Vaikundam
  • 588
  • 4
  • 20