简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM