user1154920
user1154920

Reputation: 465

Set checkmark in button on click

I have a button which when clicked shows a checkmark image just inside the right side of the button. What's the best way to show this check mark? I was hoping to just hide the image and when clicked display it, but I can't get it to display in the proper spot. I also saw android:drawableRight in the documentation, but is there a way to hide that until clicked?

xml for button and checkmark image

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:onClick="myClickHandler"
    android:id="@+id/btn"/>
<ImageView
    android:layout_height="24px"
    android:layout_width="24px"
    android:id="@+id/check"
    android:src="@drawable/check_mark"
    android:visibility="gone"
    />

Thanks

Upvotes: 0

Views: 1438

Answers (1)

Damian
Damian

Reputation: 8072

You need to do this:

Button btn = (Button)getViewById(R.id.btn);
Drawable drawable = getResources().getDrawable(R.drawable.chec_mark);

//hide drawable with this call
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null); //order of params (left, top, right, bottom)

//show drawable on right side of button with this call (in your onclick method)
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null);

Upvotes: 1

Related Questions