How do I make an ImageButton image change its image when you press it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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.