Reputation: 1272
Is there a way to add a pressed state to an ImageView? I have an image that i place a click listener on, and when I press it I want to change to imageview src for a second to mimic the pressed state of buttons or listview items.
Can I add a selector xml to the src attribute?
Figured it out. You CAN add a selector xml to your src attribute of an ImageView. In my case, i created "addbuttonbg.xml" in my drawables:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/quickaddbutton" />
<item android:state_pressed="true"
android:drawable="@drawable/quickaddbutton_click" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/quickaddbutton" />
</selector>
Then set your imageview src to @drawable/addbuttonbg
Upvotes: 3
Views: 6093
Reputation: 1835
Yes you can add a selector to get the press effect
Sample:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/postbutton_press" android:state_pressed="true"/>
<item android:drawable="@drawable/postbutton_press" android:state_focused="true"/>
<item android:drawable="@drawable/postbutton_normal"/>
</selector>
To the ImageView's android:src="@drawable/post_btn_click" attribute or android:background="@drawable/post_btn_click" to a button attribute
post_btn_click -> selector drawable's file name
Upvotes: 9