Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar MenuItem selector [closed]

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 !

like image 850
NewestStackOverflowUser Avatar asked Dec 12 '22 05:12

NewestStackOverflowUser


1 Answers

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>
like image 161
adneal Avatar answered Jan 16 '23 20:01

adneal