Search code examples
androiddrawableandroid-drawable

Android programmatically change drawableRight


How I can change programmatically drawableRight image ?

This is my Button with the drawableRight, Already set with drawableRight image -

    <Button
    android:id="@+id/button"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:layout_marginStart="11dp"
    android:backgroundTint="#496a4a"
    android:drawableRight="@drawable/ic_add_white_24px"
    android:textColor="@android:color/white"
    android:text="Shop"
    android:layout_alignBaseline="@+id/button"
    android:layout_alignBottom="@+id/button3"
    android:layout_toEndOf="@+id/button2" />

And here I wont to change the drawableRight image -

public void onClick(View v){
    if(v.getId()==R.id.button){
        //Log.d(TAG,"button");
        // Need to change the drawableRight image !
    }
}

Solution

  • button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);