In this tutorial we will learn about how to create checkbox in android studio, and show a toast message of checkbox value when user select it and checkbox example .

Android checkbox is used to show the multiple options to select the user. Android checkbox is allow the user to select one or more than one options from a group of options at a time.

A checkbox is a button with two states, checked and unchecked. Unlike radio buttons, changes in the states of one checkbox do not usually affect other checkboxes. CheckBox is an android widget, that belong to android.widget.CheckBox class.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a CheckBox to your main layout file :

We create a CheckBox to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"/>

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:layout_margin="4dp"
    android:orientation="vertical">   

    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"/>
    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Java"/>
    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Php"/>
    <CheckBox
        android:id="@+id/checkbox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mysql"/>
    <CheckBox
        android:id="@+id/checkbox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Json"/>

</LinearLayout>

3. Implement onClickListener on CheckBox :

Now set onClickListener of CheckBox to perform an action or event, like toast message in MainActivity.java file or just add the following code .

checkBox1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox1.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox1.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox1.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });

MainActivity.java :

package net.technxt.testapp;

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

public class MainActivity extends AppCompatActivity {

    CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkBox1 = (CheckBox)findViewById(R.id.checkbox1);
        checkBox2 = (CheckBox)findViewById(R.id.checkbox2);
        checkBox3 = (CheckBox)findViewById(R.id.checkbox3);
        checkBox4 = (CheckBox)findViewById(R.id.checkbox4);
        checkBox5 = (CheckBox)findViewById(R.id.checkbox5);

        checkBox1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox1.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox1.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox1.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        checkBox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox2.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox2.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox2.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        checkBox3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox3.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox3.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox3.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        checkBox4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox4.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox4.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox4.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
        checkBox5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox5.isChecked()){
                    Toast.makeText(MainActivity.this, checkBox5.getText()+" checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, checkBox5.getText()+" Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}

Now run the project/app to test CheckBox in android app through android device or any Emulator .

%d bloggers like this: