0

How to show that ImageButton is pressed if i add image to it like this:

        Drawable testPic = getResources().getDrawable(R.drawable.test_pic);

        button.setImageDrawable( testPic ); 

And make transparent background so that there would be just image:

button.setBackgroundColor(Color.TRANSPARENT); 

So when i press image button i don't see that it is pressed. I want that image would be highlighted or something when it is pressed.

Also every my button is created dynamically by code and i don't know what image would be and how many button there are.

So any ideas ?

Thanks.

Streetboy
  • 4,351
  • 12
  • 56
  • 101
  • I think this should solve your problem: [ImageButton selected sate][1] [1]: http://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state – MLoydd May 07 '12 at 07:26

3 Answers3

2

you should use a selector for choosing all the different appearances of a UI element, including an ImageButton.

first comes the state_Pressed=true which means what does it look like when pressed. after that comes the regular which is the normal state of the button.

thepoosh
  • 12,497
  • 15
  • 73
  • 132
  • Is selector link relevant with answer? – Paresh Mayani May 07 '12 at 07:23
  • @PareshMayani, thanks for the message, changed the link to a more relevant one... – thepoosh May 07 '12 at 07:26
  • +1 Now its true link with Perfect Example. @Streetboy check this and you may like it and accept this as correct answer. – Paresh Mayani May 07 '12 at 07:28
  • Again i don't have images and i don't know what they would look like. I just want to some how highlight any image – Streetboy May 07 '12 at 08:30
  • well, than you have to create different images for every state. that's how it works. (you can make a different background color for each state using the same image) – thepoosh May 07 '12 at 08:32
  • What if i want transparent background color as i want to see just image without ImageButton background. And then want top highlight image itself. It is imposable ? Just though, maybe i can on press add transparent black image on top of image i pressed ? But there should be a better way – Streetboy May 07 '12 at 08:40
  • everything is doable. all you need to do is use what you have and manipulate it as wanted. that is different question though... – thepoosh May 07 '12 at 08:42
1

I think you should use State List for that...

C. Leung
  • 6,218
  • 3
  • 20
  • 32
0

You have to change Image at run time. You can achieve these with the help of xml file.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/category_listing_bg_img" /> 
<item android:state_pressed="true"   android:drawable="@drawable/category_listing_bg_img_pressed" /> 
</selector>

Copy this xml to your drawable folder

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62