Custom Checkbox component instances in Android

  • 2020-06-12 10:34:22
  • OfStack

In Android, Checkbox is a very important UI component, and in Android it is becoming more and more attractive, which means that some systems, such as checkbox below 4.0, are still less attractive, or do not match the style of the software, so we need to customize this component.

Customizing this component is as simple as adding and modifying the xml file.

The preparatory work

Prepare two images, one selected and one unselected. This paper takes checked. png and unchecked. png as examples.

Set selection box

Create a new file custom_checkbox.xml under drawable


<?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/checked"></item>
  <item android:state_checked="false" android:drawable="@drawable/unchecked"></item>
  <item android:drawable="@drawable/unchecked"></item><!-- The default one -->
</selector>

Apply customization

Set the value of the button property to custom_checkbox as defined above.


<CheckBox
  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_checkbox"
/>

Custom finished, run your program.


Related articles: