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.
MainActivity.java
//Below is the code for MainActivity.java package com.neveropen.gfg.checkbox; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.Toast; public class MainActivity extends AppCompatActivity { CheckBox ch, ch1, ch2, ch3; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // Binding MainActivity.java with activity_main.xml file setContentView(R.layout.activity_main); // Finding CheckBox by its unique ID ch=(CheckBox)findViewById(R.id.checkBox); ch1=(CheckBox)findViewById(R.id.checkBox2); ch2=(CheckBox)findViewById(R.id.checkBox3); ch3=(CheckBox)findViewById(R.id.checkBox4); } // This function is invoked when the button is pressed. public void Check(View v) { String msg= "" ; // Concatenation of the checked options in if // isChecked() is used to check whether // the CheckBox is in true state or not. if (ch.isChecked()) msg = msg + " Painting " ; if (ch1.isChecked()) msg = msg + " Reading " ; if (ch2.isChecked()) msg = msg + " Singing " ; if (ch3.isChecked()) msg = msg + " Cooking " ; // Toast is created to display the // message using show() method. Toast.makeText( this , msg + "are selected" , Toast.LENGTH_LONG).show(); } } |
activity_main.xml
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.
<!-- Below is the code for activity_main.xml --> <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#ffffff" android:orientation = "vertical" tools:context = "com.example.hp.checkbox.MainActivity" > < TextView android:id = "@+id/textView" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" <!--create 8dp space from margin ends--> android:layout_marginEnd="8dp" <!--create 8dp space from start of margin--> android:layout_marginStart="8dp" <!--create 48dp space from the top of margin--> android:layout_marginTop="48dp" android:text="Choose your hobbies:" android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> < CheckBox android:id = "@+id/checkBox" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" android:text="Painting" android:layout_marginTop="16dp" android:textSize="18sp" /> < CheckBox android:id = "@+id/checkBox2" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" android:text="Reading" android:layout_marginTop="16dp" android:textSize="18sp" /> < CheckBox android:id = "@+id/checkBox3" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Singing" android:textSize="18sp" app:layout_constraintTop_toTopOf="@+id/textView" tools:layout_editor_absoluteX="382dp" /> < CheckBox android:id = "@+id/checkBox4" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" android:text="Cooking" android:layout_marginTop="16dp" android:textSize="18sp" app:layout_constraintTop_toBottomOf="@+id/checkBox" tools:layout_editor_absoluteX="386dp" /> < Button android:id = "@+id/button" <!-- covers the entire width of the screen --> android:layout_width="match_parent" <!-- covers as much height as required. --> android:layout_height="wrap_content" android:layout_marginTop="16dp" android:onClick="Check" android:text="submit" /> </ LinearLayout > |
Output: