CheckBox belongs to android.widget.CheckBox class. Android CheckBox class is the subclass of CompoundButton class. It is generally used in a place where user can select one or more than choices from a given list of choices. For example, selecting hobbies.
public class CheckBox extends CompoundButton
Class Hierarchy :
java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳ android.widget.Button ↳ android.widget.CompoundButton ↳ android.widget.CheckBox
It has two states – checked or unchecked.
Methods of CheckBox class
- public boolean isChecked(): If CheckBox is in checked state then return true otherwise false.
- public void setChecked(boolean status): It changes the state of the CheckBox.
Below is the code for an example where the user chooses its hobbies from the given list containing Painting, Reading, Singing and Cooking with the help of CheckBox.
The activity_main.xml has a TextView, 4 CheckBoxes and a button.The TextView prompts the user to select his/her hobbies.
First user select its choices and then presses the Submit button. After pressing Submit button, a toast will generate showing the selected hobbies.
- Android | How to Create/Start a New Project in Android Studio?
- Android | Android Application File Structure
- Android | AdMob Banner Ads for Android Studio
- Android | How to add Radio Buttons in an Android Application?
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Request permissions in Android Application?
- Android | Running your first Android app
- JavaFX | Checkbox
- How to add ColorSeekBar in Android
- How to add a Snackbar in Android
- Android Menus
- Android UI Layouts
- Android | 8.0 Oreo
- Android SearchView with Example
- How to use WebView in Android