Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change button's drawableLeft when the button is clicked

I have a Button with a drawableLeftand when i click on it i want to change only the drawableleft for another image. What java code i have to use?

XML:

<Button
    android:id="@+id/docli_btn_apagar"
    android:layout_width="200dp"
    android:layout_height="70dp"
    android:background="@android:color/transparent"
    android:onClick="ApagarLinha"
    android:drawableLeft="@drawable/trash"
    android:text="@string/apagar" 
    android:textAppearance="?android:attr/textAppearanceLarge"/>
like image 944
Celta Avatar asked Oct 29 '25 05:10

Celta


1 Answers

This would be called in your button's onClick() handler

// get your button
Button docli_btn_apagar = (Button) findViewById(R.id.docli_btn_apagar );

// get the drawable
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );

// set the drawable left
docli_btn_apagar.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );

Or alternatively:

// You can skip the drawable object directly, by using ints/IDs
docli_btn_apagar.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
like image 175
Bryan Denny Avatar answered Oct 30 '25 22:10

Bryan Denny



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!