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:android="http://schemas.android.com/apk/res/android"
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:
Similar Reads
How to Check Screen Time in Android? In today's Digital World, individuals spend most of their time on Smartphones. Hence, it increases Android Screen Time which is one of the prominent reasons for Poor Eye Site.So, if you want a Healthy Eye Site, you have to Check Screen Time Usage on Android daily and try to reduce it. In this articl
5 min read
How to Make CardView Checkable In Android? In Android, We can make a CardView checkable, which can be really a useful feature. If we want the user to select some items and want to display the items that the user has chosen then this one is the most important feature for us. A sample GIF is given below to get an idea about what we are going t
4 min read
GravityView in Android In this article, we are going to show the GravityView in android. In this article, we are going to see the gravity effect on an image. As we move our phone we will see different parts of the image. Here we will be using Horizontal ScrollView so we will be moving our phone horizontally. In the below
2 min read
How to Check if CheckBox is Checked in Android? Checkbox is a UI element mostly found in websites and applications around us. They are square-shaped boxes ahead of a choice or a statement. When a user clicks on it, they get ticked. They are most commonly found in electronic forms in websites and applications where the user needs to make one or ma
3 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read