Check Box in Android Example

The Check Box element has been here for a very long time, so they can be seen in not just Android applications but websites and many other platforms as well. Yes, it is small; some people might not even notice it on a page at first glance, but a Check Box is one of the most important UI (User Interface) elements in Android.

Although the versatility of the Check Box is unrivalled in Android development, it is widely mixed up with another UI tool of Android. So beginners, UI designers and even app developers are often led to a number of misconceptions about the Check Box element. Therefore, it is a must for people in this field to study Check Boxes.

In this tutorial, not only will we show you how to create a Check Box in Android, but we will also remove and prevent all the confusions regarding the Check Box tool.

Check Box in Android

In Android, a Check Box is nothing but a small square box. This small box is actually a button, which is toggled every time a user clicks on it. Naturally, the Check Box element has two states: checked and unchecked. These states can also be referred to as on (checked) and off (unchecked). When a user clicks on an unchecked Check Box, the empty Check Box gets filled with a check mark, which is also known as a tick mark. This is the checked state. In the same way, clicking on a checked Check Box removes the check mark in it and sends it to the unchecked state. In its default state, a Check Box is usually set off or “unchecked.” However, it can be marked on or “checked” by setting the attribute android:checked=”true” in the XML layout file.

A Check Box is a valuable tool because it serves an important purpose of an Android app developer. In Android development, a developer uses the Check Box widget when he needs to present one option or a group of options, all of which are selectable at a given time and not mutually exclusive. In addition to that, before implementing the Check Box class, one must always remember that the Compound Button class is its parent class. Apart from that, another noteworthy thing about the Check Box class is that every Check Box must register for click listener.

Check Box vs Radio Button

Even though there are only a few similarities between a Check Box and a Radio Button, people often get confused between the two. The main similarity is that they are both small buttons that allow their users to choose an option. Also, they both have the checked and unchecked states.

However, unlike a Check Box, a Radio Button fills up with a dot in its checked state. Other than that, while Check Boxes are independent, Radios Buttons provide mutually exclusive options to select. Because Radio Buttons are not mutually exclusive, they are implemented as a group called a Radio Group. This is why a user has to select one option in the case of Radio Buttons; however, all Check Box options are selectable.

For these reasons, a Check Box and a Radio Button are very different from each other and can’t be used interchangeably. An example of Radio Button application can be seen on the sign up page of an app where you are asked to enter your gender. You are given two options: Male and Female, and there is a Radio Button beside each option.

Check Box Applications and Examples

Due to its important features and versatility, an Android Check Box has numerous applications. The Check Box element can be seen in forms and databases in order to indicate possible options for a question. Also, the Check Box widget is also applied to a collection of settings. Additionally, it may be used to allow a user to make multiple selections in a single list. If not that, it may be used to apply a single Check Box for making single selections.

Examples of Check Box application are seen in nearly every Android app. You may see a Check Box where a Boolean True or False statement is present, such as “Is two greater than three? Yes or No.” Another example of Check Box usage is mostly seen in the main activity of social media apps. In these apps, when you launch the application, there is a “Remember me” option accompanied with a Check Box in the Login form.

Also, a Check Box is seen under the “Terms and Conditions” portion in an Android app. The Check Box is present beside the option that basically says “I agree.”

Repeated usage of Check Boxes can be seen on food delivery apps, such as Uber Eats. Here, Check Box options allow a user to personalize their order. The options with the list of Check Boxes can be regarding extra toppings, special dietary requirements, etc.

Every Check Box must register for click listener. CompoundButton is the parent class for the CheckBox class.

Check Box is generally used when you want user to select one or more items from the list. Example you ask user their favorite food to select among many. In this case user can select one or many according to his likes. By default Check Box is set “OFF”. You can also marked Check Box “ON” by setting android:checked =”true” in XML layout file.

We can also create Check Box in activity file.

Check Box in Android Steps

In order to create a Check Box for an Android application, the first step for you is to open the Android Studio IDE (Integrated Development Environment). Then, you have to create a project on Android Studio as we have shown in our first tutorials.

In below example we ask users to select their favorite color. As you can see from output two colors selected, one is Blue and other one Red. Click on submit button to Toast the selected items.

Now, open the MainActivity.java class file of your project in order to enter the following code:

Using this code, we have asked a user to select his favourite color. Under this question, we have placed a set of Check Boxes with options that define names of different colors, such as Blue, Green and Yellow. We have also used this code to display a Toast message whenever the user selects his options and presses the “SUBMIT” button. This Toast message will read “Your favourite color : ” followed by the options the user has chosen, such as “Yellow Red.”

Once you have completed your coding, launch your project to display your output. If you have correctly entered the above-mentioned codes, you would get the following output:

Output

check box in android