top banner


A CheckBox example in Android Studio in XML layout can be used to allow the user to make a binary choice between two options. Here’s how to use it.

  1. Add the CheckBox to your layout XML file.
  2. Set the layout_width and layout_height properties to control the size of the CheckBox. You can set them to wrap_content to make the CheckBox the same size as its text, or set them to a specific value in pixels or dp to control the size.
  3. Set the text property to specify the text that should be displayed next to the CheckBox, to describe the option.
  4. You can also set additional properties, such as textColor to control the color of the text, and checked to specify whether the CheckBox should be checked or not by default.

Here’s an example of a CheckBox in XML layout in Android Studio that displays text next to a checkbox and sets its size to match the size of the text:

<?xml version="1.0" encoding="utf-8"?>
android:text="Option 1" />

Note: To handle the state of the CheckBox in your code, you can use the isChecked() method on the CheckBox object to determine whether the CheckBox is checked or not. For example:

CheckBox myCheckBox = findViewById(;
boolean isChecked = myCheckBox.isChecked();

That’s it! You can now run your app and see the CheckBox displayed in the layout. The user can check or uncheck the CheckBox, and you can access the state of the CheckBox in your code.

Leave a Reply

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