Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView setClickable(true) ... setPressed(true) not staying pressed

Tags:

android

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?

like image 979
LuxuryMode Avatar asked Jun 22 '26 20:06

LuxuryMode


1 Answers

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.

like image 170
Xavi Gil Avatar answered Jun 24 '26 11:06

Xavi Gil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!