Checkboxes in Android are a great widget that you can use in apps like a To Do list, Food ordering, selling insurance, etc.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

To create a checkbox in the XML file we use the <CheckBox> tag with layout_width and layout_height as required attributes and few other necessary attributes like text, textSize, textColor, buttonTint, id, and required margins.

Here is an example code of using a checkbox in Android:

 <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="68dp"
        android:buttonTint="@color/colorPrimary"
        android:text="Java"
        android:textColor="@color/colorAccent"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

Checkboxes in android has either of the two states: checked or unchecked. It also inherits many methods of a View, TextView and Button class. However to check if a checkbox is checked or unchecked we call the following method:

checkbox.isChecked();

and to set it check or uncheck:

checkbox.setChecked(true); //set it true or false, true for checked.

Checkboxes in Android Example

In the following example we will build an app that’ll allow user to add few programming languages to its To Do list for the current year.

Checkboxes In Android
Checkboxes In Android

To achieve a view as shown above, we have written following XML Code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="8dp"
        android:text="Languages to learn"
        android:textAlignment="center"
        android:fontFamily="monospace"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="68dp"
        android:buttonTint="@color/colorPrimary"
        android:text="Java"
        android:textColor="@color/colorAccent"
        android:textSize="15sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="28dp"
        android:text="Python"
        android:buttonTint="@color/colorPrimary"
        android:textSize="15sp"
        android:textColor="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="28dp"
        android:text="Dart"
        android:buttonTint="@color/colorPrimary"
        android:textSize="15sp"
        android:textColor="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="28dp"
        android:text="Kotlin"
        android:buttonTint="@color/colorPrimary"
        android:textSize="15sp"
        android:textColor="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox3"
        tools:layout_editor_absoluteX="171dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="8dp"
        android:backgroundTint="@color/colorAccent"
        android:textColor="#FFF"
        android:text="Add to this year ToDos"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox4" />

</android.support.constraint.ConstraintLayout>

Here is the java code for to do list

package com.example.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CheckBox kotlin,java,dart,python;
    Button todosAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        java= findViewById(R.id.checkBox);
        python= findViewById(R.id.checkBox2);
        dart= findViewById(R.id.checkBox3);
        kotlin= findViewById(R.id.checkBox4);
        todosAdd = findViewById(R.id.button);

        //listener for checkboxes
        java.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(java.getText());
            }
        });

        python.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(python.getText());
            }
        });

        dart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(dart.getText());
            }
        });

        kotlin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(kotlin.getText());
            }
        });

        //On button click appending todo string
        todosAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder todo = new StringBuilder();
                if(java.isChecked()){
                    todo.append("Java\n");
                }
                if(kotlin.isChecked()){
                    todo.append("Kotlin\n");
                }
                if(python.isChecked()){
                    todo.append("Python\n");
                }
                if(dart.isChecked()){
                    todo.append("Dart\n");
                }
                showToast(todo);
            }
        });

    }

    private void showToast(CharSequence text) {
  Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
    }
}

Voila! and here is all that you need to know about implementing a Checkbox in your Android Studio project!

Interested in learning how to change the color theme of your app? Here is a blog on that: Android Change Theme Programmatically using ColorPrefUtil

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like