Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change button image in android

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?

like image 381
user195678 Avatar asked Feb 08 '11 21:02

user195678


People also ask

How will you add graphic to button?

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.

What is Android ImageButton?

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.

How do I make a clickable image button?

To avoid confusion rename your button android:onClick="onClick" , like android:onClick="imageButtonOnClick" . Thank you!


1 Answers

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);
    }       
}
like image 90
swiftBoy Avatar answered Sep 24 '22 15:09

swiftBoy