Wednesday, July 3, 2024
HomeLanguagesJavaCheckBox in Android

CheckBox in Android

CheckBox is used for adding multiple selections of items from the given set of options. This is seen used in many android applications for adding a feature for multiple selections. In this article, we will take a look at How to implement Checkbox in Android. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVStatus"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Checkbox in Android"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <!--on below line we are creating a text view-->
    <TextView
        android:id="@+id/idTVStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idCheckBox"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Status"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <!--on below line we are creating a checkbox-->
    <CheckBox
        android:id="@+id/idCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Check the box" />
 
</RelativeLayout>


Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail.

Kotlin




package com.gtappdevelopers.kotlingfgproject
 
import android.os.Bundle
import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
 
    // on below line we are creating a variable.
    lateinit var checkBox: CheckBox
    lateinit var statusTV: TextView
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // on below line we are initializing our variables.
        checkBox = findViewById(R.id.idCheckBox)
        statusTV = findViewById(R.id.idTVStatus)
 
        // on below line we are checking if check box ix checked.
        if (checkBox.isChecked) {
            // on below line we are updating text
            // if check box is checked.
            statusTV.text = "Checkbox is Checked."
        } else {
            // on below line we are updating text
            // if check box is unchecked.
            statusTV.text = "Checkbox is UnChecked."
        }
 
        // on below line we are adding check change listener for our check box.
        checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
            // on below line we are checking
            // if check box is checked.
            if (isChecked) {
                // on below line we are updating text
                // if check box is checked.
                statusTV.text = "Checkbox is Checked."
            } else {
                // on below line we are updating text
                // if check box is unchecked.
                statusTV.text = "Checkbox is UnChecked"
            }
        }
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
 
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    // on below line we are creating a variable.
    private CheckBox checkBox;
    private TextView statusTV;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // on below line we are initializing our variables.
        checkBox = findViewById(R.id.idCheckBox);
        statusTV = findViewById(R.id.idTVStatus);
 
        // on below line we are checking
        // the status of check box
        if (checkBox.isChecked()) {
            // on below line we are setting text
            // if check box is checked.
            statusTV.setText("Checkbox is Checked");
        } else {
            // on below line we are setting the text
            // if check box is un checked
            statusTV.setText("Checkbox is UnChecked");
        }
 
        // on below line we are adding check change listener for our check box.
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // on below line we are checking if
                // checkbox is checked or not.
                if (isChecked) {
                    // on below line we are setting text
                    // if checkbox is checked.
                    statusTV.setText("Checkbox is Checked");
                } else {
                    // on below line we are setting text
                    // if checkbox is unchecked.
                    statusTV.setText("Checkbox is UnChecked");
                }
            }
        });
    }
}


Now run your application to see the output of it. 

Output:

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments