5

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 !

NewestStackOverflowUser
  • 2,792
  • 5
  • 22
  • 31

1 Answers1

15

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>
adneal
  • 30,484
  • 10
  • 122
  • 151