Touch feedback on menu item with actionLayout

2.1k views Asked by At

I'm using a menu item with actionLayout because I need to add a textView along with my icon for the item on the ActionBar. everything shows up correctly but I don't know (and couldn't find anywhere) how to add the animation that shows up when user taps on a menu item on Lollipop devices.

I would like to show number of comments on the side of comment icon in my toolbar.

Here is what I have:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_like"
        android:icon="@drawable/list_discuss_like"
        android:title="Like"
        app:showAsAction="always"/>
    <item
        android:id="@+id/menu_comment"
        app:actionLayout="@layout/menu_item_comment"
        android:title="Comment"
        android:icon="@drawable/ic_comment_white_24dp"
        app:showAsAction="always"/>
</menu>

and for my menu_item_comment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:clickable="true"
              android:hapticFeedbackEnabled="true"
              android:orientation="horizontal">

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

    <TextView
        android:id="@+id/ab_comment_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="(12)"
        android:textColor="@color/grayTextHint"/>

</LinearLayout>

The animation and feedback works on the like item but not on the comment one.

Any help is appreciated.

1

There are 1 answers

3
alanv On BEST ANSWER

Set the clickable element's background to the theme's action bar item background.

<LinearLayout
        ...
        android:clickable="true"
        android:background="?android:attr/actionBarItemBackground">
    ...
</LinearLayout>