Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 432
user788511 Avatar asked Sep 08 '11 03:09

user788511


1 Answers

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.

like image 182
PH7 Avatar answered Nov 18 '22 15:11

PH7