Is it possible to change the Image of a MenuItem when pressed and can that be done by a selector (different MenuItems should be changed with a different image when pressed or selected).
Some sample code would be nice.
I looked up a lot of solutions but not many of them made a clear explanation. Cheers !
Is it possible to change the Image of a MenuItem when pressed and can that be done by a selector
Yep, absolutely. Here's an example:
Add a selector to your drawable
folder.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed state -->
<item android:drawable="@drawable/ic_action_your_pressed_icon" android:state_pressed="true"/>
<!-- Default state -->
<item android:drawable="@drawable/ic_action_your_default_icon"/>
</selector>
Apply the selector to your MenuItem
using the icon
attribute:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:icon="@drawable/your_menu_item_selector" ... />
</menu>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With