Creating Android Custom Component: Check Box

Suppose, you are developing a data driven application of something related settings or kind of stuff, and where you want to implement on-off or show-hide kind of functionality. To implement such kind of functionalities we use Check Box or On-Off button components as front end. Most of the programming languages provides us some built-in components with default UI. Like you can find in while developing Android application. See the Image Below.

How to create android custom component

Well…! you have the components/widgets with default UI. But what about If you need a Check Box with Custom UI and Functionalities. So let’s see how we can do it.


Table Of Content

  1. Setting up project and assets
  2. Building CheckBox Interface
  3. Custom Methods
  4. Implement of Touch Event
  5. Complete Class
  6. How to use

Setting up project and assets

Create a new project named “Custom CheckBox” with Main.java and main.xml with white background layout.

Main.java

main.xml

Create a new folder under res>drawable-nodpi and add Check Box Graphics which we are going to use in further component development.

checked.png

How to create android custom component

Building Checkbox Interface

Now start building interface by extending View Class. Create a Custom Checkbox class named CCheckBox and extend it with View Class. See the code below.
CCheckBox.java

Setup the width and height of the view when it will create or resize.

Now draw the custom graphics of checkbox on Canvas.

Custom Methods

Create getter and setter functions to get the state of the CheckBox.

Implement of Touch Event

Now add the checked and unchecked logic under TouchEvent handler

Complete Class

CCheckBox.java

How to use

Open main.xml from res>layouts and add the CCheckBox widget which you have created. See the code below.
main.xml

Run the application and see the result.

Creating custom checkbox in android

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *