Ripple Animation not shown on MenuItem while programatically perform MenuItem click

130 Views Asked by At

I have a search feature in my app, where I can move up and down icons in the Menu. If I tap on the up and down icon I can see the ripple effect on the icon. Now I want to perform up icon functionalities when I tap the search button from the keyboard. To achieve this programmatically I made up icon click action. I can get up icon functionalities but I can't make a ripple effect on the icon. Any workaround to make a ripple effect on the icon?

Below code, I have used to make the menu programmatically clicked.

 searchEdit.setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
             actionUpMenuItem?.let {
                 onOptionsItemSelected(it)
             }
           true
        } else {
            false
        }
    }

and also I tried this, I can't see the ripple effect in both function

searchEdit.setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
           optionMenu?.performIdentifierAction(R.id.action_up, 0)
           true
        } else {
            false
        }
    }

This is the menu item code

<?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/action_up"
    android:foreground="?attr/selectableItemBackground"
    android:icon="@drawable/ic_search_up_arrow"
    android:orderInCategory="94"
    android:title="@string/description_item_up"
    app:showAsAction="always" />

<item
    android:id="@+id/action_down"
    android:foreground="?attr/selectableItemBackground"
    android:icon="@drawable/ic_search_down_arrow"
    android:orderInCategory="95"
    android:title="@string/description_item_down"
    app:showAsAction="always" />
</menu>
1

There are 1 best solutions below

0
On

Create your custom ripple effect ripple_effect_small_corner

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="?android:attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
            <corners android:radius="@dimen/_2sdp" />
        </shape>
    </item>
    <item android:drawable="@drawable/list_item_background_small_corner" />
</ripple>

create drawable list_item_background_small_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/whitecolor"/>
    <stroke android:width="1dp" android:color="@color/divider" />

<corners android:radius="@dimen/_2sdp"></corners>

</shape>

You can use like this:

android:background="@drawable/ripple_effect_small_corner"