Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace/change image button programmatically android

I have an image button on my view which i need to change after user interaction. I dont find nothing like myImageButton.setDrawable Here is my xml for the button i want to change:

<ImageButton
        android:id="@+id/stopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/buttons_background"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:background="@null"
        android:src="@drawable/btn_play" />

The only line I need to change android:src="@drawable/btn_play" to android:src="@drawable/btn_stop" programmatically. Can this be done? Thanks in advance.

like image 223
madcoderz Avatar asked May 13 '13 15:05

madcoderz


People also ask

How to disable image button in android?

If you want make enable = false in XML for ImageButton you have to add android:focusable="false" and android:clickable="false" . In fact, setting android:enabled to "false" DOES disable an ImageButton; the button doesn't receive lick events anymore.

How to set image in image button?

An image on the surface of a button is defined within a xml (i.e. layout ) by using src attribute or within java class by using setImageResource() method. We can also set an image or custom drawable in the background of the image button.

How to set image button in Android Studio?

Navigate to the app > res > drawable. Right-click on the drawable folder and open it in explorer. Paste the copied image here and close it. You will now be able to locate your image inside the drawable folder in android studio.

How to add image button in android?

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.


1 Answers

You can use

setImageResource for the image button
like image 138
Jithu Avatar answered Sep 28 '22 19:09

Jithu