I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button and the button change to a new image. Can this be done?
Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file.
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states.
To avoid confusion rename your button android:onClick="onClick" , like android:onClick="imageButtonOnClick" . Thank you!
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With