Checkboxes In Android

This article explains Checkboxes in Android. Android Studio is used to create the sample.

Introduction

This article explains Checkboxes in Android. Android Studio is used to create the sample.

CheckBox

A CheckBox is a type of two-state button that can either be checked or unchecked.

In this article I used three CheckBoxes that, when clicked, display a toast that contains the id of that checkbox. If you check the first checkBox then it displays checkbox1 and if you check the second CheckBox then the toast displays checkbox2. I have also used a button so when clicking the button the state of the checkboxes display on the screen that shows which TextBox is selected. If one TextBox is selected then it shows checkbox1:true and another with false. If all the checkboxes are selected then it shows checkbox1:true, checkbox2:true, checkbox3:true.

Step 1

Create a project like this:

Clipboard06.jpg

Step 2

Create an XML file and the following.

In this I used three checkboxes and a button that show the state of all the checkboxes on a click.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:background="#83a697">

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Check Box Example"

        android:layout_centerHorizontal="true"

        android:textStyle="bold"

        android:textColor="#00ffff"

        android:textSize="30dp" />

 

 

    <CheckBox

        android:id="@+id/checkbox1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="70dp"

        android:text="Check1"

        android:textStyle="bold"

        android:textColor="#00ffff" />

 

 

    <CheckBox

        android:id="@+id/checkbox2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="140dp"

        android:text="Check2"

        android:textStyle="bold"

        android:textColor="#00ffff" />

 

    <CheckBox

 

        android:id="@+id/checkbox3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="210dp"

        android:text="Check3"

        android:textStyle="bold"

        android:textColor="#00ffff" />

 

    <Button

        android:layout_width="wrap_content"

        android:id="@+id/buttonDelete"

        android:layout_height="wrap_content"

        android:layout_marginTop="300dp"

        android:layout_centerHorizontal="true"

        android:textColor="#00ffff"

        android:text="Show Message" />

 

</RelativeLayout>

 

Step 3

 

Create a Java class file and write the following.
 

First I created the layout, the id of the checkboxes and button. Now set checkbox1 on its clickListener() and oncheck get the state of the checkbox.  I also used the button to show the state of the checkboxes.


package com.checkboxexample;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Gravity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

    CheckBox checkBox1, checkBox2, checkBox3;

    Button buttonDelete;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        buttonDelete = (Button) findViewById(R.id.buttonDelete);

        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);

        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);

        checkBox3 = (CheckBox) findViewById(R.id.checkbox3);

 

        checkBox1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

 

                if (checkBox1.isChecked()) {

                    checkBox1.getText().toString();

                    Toast.makeText(getApplicationContext(), checkBox1.getText().toString(), Toast.LENGTH_LONG).show();

                }

            }

        });

        checkBox2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                checkBox2.getText().toString();

                Toast.makeText(getApplicationContext(), checkBox2.getText().toString(), Toast.LENGTH_LONG).show();

 

            }

        });

 

        checkBox3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                checkBox3.getText().toString();

                Toast.makeText(getApplicationContext(), checkBox3.getText().toString(), Toast.LENGTH_LONG).show();

 

            }

        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                StringBuffer buffer = new StringBuffer();

                buffer.append("checkBox1 :").append(checkBox1.isChecked());

                buffer.append("\ncheckBox2 :").append(checkBox2.isChecked());

                buffer.append("\ncheckBox3 :").append(checkBox3.isChecked());

 

                Toast toast=new Toast(getApplicationContext());

           toast.makeText(getApplicationContext(),buffer.toString(),Toast.LENGTH_LONG).show();

            toast.setGravity(Gravity.CENTER_VERTICAL,0,30);

            }

        });

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

}

Step 4

Output image when no checkbox is checked:

2.jpg
 

 Step 5

When one checkbox ic shecked:

Clipboard04.jpg

 

 Step 6

When two checkboxes are checked:

Clipboard006.jpg