Rohit Parmar Rohit Parmar - 11 months ago 78
Android Question

Programmatically remove the left side image of button but not on right side?

Hello i have button with two images drawable left and right,i want to remove only left side image,and want to keep the right side image my button xml code is below.

<Button
android:id="@+id/fragment_add_to_cart_btn_proceed_to_checkout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorRed"
android:drawableEnd="@drawable/ic_action_next"
android:drawablePadding="5dp"
android:drawableStart="@drawable/ic_rounded_cart"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/proceed_to_checkout"
android:textColor="@color/colorWhite" />


i have applied some code like below but its not working

button.setCompoundDrawables(null, null, null, null);


if you have any solution please reply?
thank you.

Answer Source

Do like this inside onCreate. Give image resource for which you need to show else will be given 0.

int imgResource = R.drawable.common_google_signin_btn_icon_dark;
fragment_add_to_cart_btn_proceed_to_checkout = (Button) findViewById(R.id.fragment_add_to_cart_btn_proceed_to_checkout);
fragment_add_to_cart_btn_proceed_to_checkout.setCompoundDrawablesWithIntrinsicBounds(0, 0, imgResource, 0);