I've got an ImageView which I'm setting to setImageResource(R.drawable.someStateListDrawable). Everything works fine, when it's clicked, it shows the pressed state. However, I've made it so that it onClick, the ImageView is set to "setPressed(true)" so that it will remain in the pressed state. But for some reason, its not... Any ideas?
Asked
Active
Viewed 3,639 times
3
LuxuryMode
- 33,401
- 34
- 117
- 188
2 Answers
7
I just had this same problem with a Button. When onClick I set it to the pressed state ( btn.setPressed(true) ) but after that the button wasn't pressed.
In case this helps somebody, I finally worked with the selected state. My drawable xml looks like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/menu_button_pressed"/>
</selector>
and at onClick I now use btn.setSelected( true ).
I don't know what was the problem with setPressed, but this worked for me.
Xavi Gil
- 11,460
- 4
- 56
- 71
-
Yeah I've since discovered this too. Thanks! – LuxuryMode Dec 16 '11 at 14:43
-
I've been struggling with this for hours! It seems Android is changing the "pressed" state both before and after onClick... Thanks for your solution – Couitchy Aug 27 '13 at 13:58
0
Have you checked out this question? He basically says use a selector, and in the xml do something like this:
<item android:state_pressed="true" android:drawable="@color/gray" />
Of course you would need to modify it to fit your needs.
-
Of course, I already defined a pressed state in my Selector. That's why it switches to the pressed state when clicked. I'd just like it to say pressed. – LuxuryMode Aug 08 '11 at 18:15