user788511
user788511

Reputation: 1736

implement android:button="@drawable/checkbox" programmatically

I am trying to create custom Check Box button image. After some research, I came across this code sample:

<CheckBox android:id="@+id/chkFav" android:layout_width="wrap_content"
        android:layout_marginRight="0dp" android:button="@drawable/checkbox"
              android:layout_height="wrap_content" android:clickable="true"/>

My query is how to actually implement android:button in code.

Upvotes: 7

Views: 8359

Answers (1)

PH7
PH7

Reputation: 3916

setButtonDrawable(Drawable d) is the way to go for. Make sure it is state-list drawable to respond to user interaction.

Step By step instruction

  1. Have at least 2 images. (one for checked state and another for normal state)
  2. Create xml drawable. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
  3. Use setButtonDrawable(R.drawable.your_xml_drawable).

**Notes-- there are many ways to achieve. This is just one simple way to do it.

Upvotes: 13

Related Questions