Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageButton change src

Change the ImageButton src

Here is the xml for the tag:

    <ImageButton
        android:layout_width="0dp"
        android:id="@+id/infobutton"
        android:layout_weight="1"
        android:src="@drawable/edit"
        android:scaleType="fitCenter"
        android:layout_height="60dp" />

This is what im doing to the button when it get pressed

        button.setBackgroundResource(R.drawable.save);

The ImageButton before pressed

enter image description here

Image after press:

enter image description here

I want the button to only have the save Picture and it beeing fitCenter and why doesnt the other src get replaced With the New src?

like image 522
Kim Vu Avatar asked Oct 29 '25 14:10

Kim Vu


2 Answers

Because android:src and android:background is the different attributes. You should use

button.setImageResource(R.drawable.save);
like image 143
Sergey Nikitin Avatar answered Nov 01 '25 05:11

Sergey Nikitin


your code is trying to change the background of the button. not its image. Those are two different things

((ImageButton) view).setImageResource(R.drawable.icon2);
like image 45
Lucifer Avatar answered Nov 01 '25 03:11

Lucifer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!