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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Is selector link relevant with answer?
@PareshMayani, thanks for the message, changed the link to a more relevant one...
+1 Now its true link with Perfect Example. @Streetboy check this and you may like it and accept this as correct answer.
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
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)
|
1

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

Comments

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

1 Comment

I can't do this because i don't know what my images would look like

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.