Checkbox code for Android Studio

Welcome. Here in this tutorial you will learn how to use checkbox in Android Studio.

Checkbox Example for Android Studio

In Android Studio, a checkbox is a type of button that is either checked or unchecked. It acts like an on off switch. It can be used when you have many options available and the user has to select from list of available options. Usually check-boxes are displayed in a vertical list. A very common example of checkbox in any android app is the Remember Me option.

 

Adding Checkbox in Android App

You can add Checkbox through the XML layout by using the <CheckBox> tag.

You can also add other attributes to your Checkbox like gravity, textColor, textSize, textStyle, background, padding, etc. They will work the same as they do inside other UI elements.

 

You can also check the present condition of a check box automatically by utilizing isChecked() function. It returns output as true when the checkbox is checked and returns false if the checkbox is not selected. The java code for example is shown below which demonstrates how to work with check-boxes.

 

An image is shown below for example purpose. It shows three check-boxes and displays text as name of the check-boxes that are selected.

 

 

The Full Java Code along with the XML layout for the above example is shown below:-

  • First Create a new project in Android Studio
  • File ⇒ New Android ⇒ Application Project
  • Then Open src -> package -> MainActivity.java and then add following code :

JAVA (MainActivity.java)

  •  Now Open res -> layout -> activity_main.xml and then add following code :

XML (activity_main.xml)

Output:

Finally, run this project.

This is how Android Checkbox looks like.

Checkbox codeCheckbox codeCheckbox code

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with these checkbox options on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your checkbox more usable and modern looking.

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.android.com/guide/topics/ui/controls/checkbox.html

Comment down below if you are facing any problems.

 

Also Read:-

Leave a Reply

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