madcoderz
madcoderz

Reputation: 4431

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.

Upvotes: 9

Views: 22297

Answers (4)

CSmith
CSmith

Reputation: 13458

Use

setImageResource(R.drawable.btn_stop);

Upvotes: 2

James
James

Reputation: 1036

Use this:

myImageButton.setImageResource(R.drawable.btn_stop);

Upvotes: 10

Celta
Celta

Reputation: 3620

ImageButton = (ImageButton)findViewById(R.id.stopButton);
btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_stop, 0, 0, 0);

Upvotes: 3

Jithu
Jithu

Reputation: 1478

You can use

setImageResource for the image button

Upvotes: 12

Related Questions