How do I make an ImageButton image change its image when you press it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I hope you find this helpful.
This can all be done in the XML.
1) Import your images for both pressed and unpressed states into the res/drawable-whichever folder
2) Make your selectors. Right click on a drawable folder and select New/Android xml file. Put in the name eg “ok_button_selector.xml” and choose “selector” as the root element from the menu below. You will need to create a different selector for each button on the screen.
3) In each selector file you need to define the image that will display when clicked, like this:
They have to be in this order as the last is the default.
4) In your layout file use the android:onClick=”myButtonClicked” method to define the buttons clicked behaviour. This saves having to use click listeners. Just make sure your java method has the same name 🙂
5) Within the ImageButton tags define the attribute android:src=”@drawable/ok_button_selector” instead of the usual image file.
Thats it! You don’t need any extra code in your java onClick method.