开发者

Create custom check boxes in android

I have an android application. There is an activity which demands 7 checkboxes but as the size of 开发者_JAVA技巧the default check boxes provided by SDK is very large and takes lots of space, I tried to develop my own customized check boxes.

For this, I have captured two images (checked and unchecked), On click of one it toggles, i.e. on click of checked it converts to unchecked and vice versa.

But I want to know is there any other way to do this..


It's little simple by placing an appropriate drawable xml file for your checkbox states with corresponding images you need to set.

place the below code in drawable named checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:drawable="@drawable/star_checked" /> <!-- pressed -->
    <item android:state_checked="false"
          android:drawable="@drawable/star_unchecked" /> <!-- focused -->
     <item android:drawable="@drawable/star_unchecked" />      
</selector>

and on your checkbox set above code to manipulate, by using android:button=@drawable/checkbox"

<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"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜