How to replace / change the image button programmatically android

I have an image button on my view that I need to change after interacting with the user. I 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 programmatically change android:src="@drawable/btn_play" is android:src="@drawable/btn_stop" . It can be done? Thanks in advance.

+8
java android android-layout android-button
source share
4 answers

you can use

 setImageResource for the image button 
+10
source share

Use this:

 myImageButton.setImageResource(R.drawable.btn_stop); 
+8
source share
 ImageButton = (ImageButton)findViewById(R.id.stopButton); btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_stop, 0, 0, 0); 
+3
source share

Use

setImageResource(R.drawable.btn_stop);

+2
source share

All Articles