This page is being accessed at exactly: 2022-06-28T03:27:55+00:0006/28/2022How to use CheckBox in Android – [RaHa] Programming
[RaHa] programming
  • Home
  • Services
  • About Us
  • Contact

tutorials.

How to use CheckBox in Android

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
  
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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>


Leave a Reply Cancel reply

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

  • More News

    • Learning the blockchain by coding your own blockchain in GO!
    • How to build a cryptomining rig: Bitcoin mining
    • How to use CheckBox in Android
    • TEXTUREVIEW: VIDEO CROPPING – FULL SCREEN VIDEO BACKGROUND IN ANDROID APPLICATIONS
    • New physics rules tested on quantum computer
    • rahaprogramming
    • January 22, 2022

    Learning the blockchain by coding your own blockchain in GO!.

    • rahaprogramming
    • January 7, 2022

    How to build a cryptomining rig: Bitcoin mining.

    • rahaprogramming
    • June 23, 2021

    How to use CheckBox in Android.

    • rahaprogramming
    • February 18, 2021

    TEXTUREVIEW: VIDEO CROPPING – FULL SCREEN VIDEO BACKGROUND IN ANDROID APPLICATIONS.

    • rahaprogramming
    • February 16, 2021

    New physics rules tested on quantum computer.

    • rahaprogramming
    • February 15, 2021

    Elon Musk’s SpaceX now owns about a third of all active satellites in the sky.

    • rahaprogramming
    • February 8, 2021

    Dogecoin Mining: How to Mine Dogecoin – Beginners Guide.

    • rahaprogramming
    • February 1, 2021

    Elon Musk Announces Neuralink Advance Toward Syncing Our Brains With AI.

    • rahaprogramming
    • February 1, 2021

    Top 10 Most Common Mistakes That Android Developers Make: A Programming Tutorial.

    • rahaprogramming
    • February 1, 2021

    Artificial Intelligence Stocks To Buy And Watch Amid Rising AI Competition.

    Community

    • Contact Us
    • Stackoverflow

    About Us

    • Our Story
    • Services
    • FAQs

    Company

    • About Us
    • Contact

    Have a Questions?

    • 228-304-9202
    • RaHaprogramming@gmail.com

    Copyright © All rights reserved