checkbox android example

CheckBox Android Tutorial – CheckBox Example

Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list.

checkbox example
checkbox example

Step 1: Open a new project name it as you like, Include Checkbox in XML or just drag and drop it.Or just use below XML code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.bytelogs.example.MainActivity">


    <CheckBox android:id="@+id/checkbox_costumes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Costumes"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="onCheckboxClicked"/>
    <CheckBox android:id="@+id/checkbox_toy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toy"
        android:layout_centerVertical="true"
        android:layout_below="@+id/checkbox_costumes"
        android:onClick="onCheckboxClicked"
        android:layout_alignLeft="@+id/checkbox_costumes"
        android:layout_alignStart="@+id/checkbox_costumes" />

</RelativeLayout>

Step 2: Override onCheckboxClicked method and use switch cases to detect check and unchecked states.

package com.bytelogs.example;

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


public class MainActivity extends AppCompatActivity {
    CheckBox costume;
    CheckBox toy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         costume = (CheckBox) findViewById(R.id.checkbox_costumes);
         toy = (CheckBox) findViewById(R.id.checkbox_toy);

    }
    public void onCheckboxClicked(View view) {
        // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.checkbox_costumes:
                if (checked){
                    Toast.makeText(this,"Checked costumes",Toast.LENGTH_LONG).show();
                }
            else{
                    Toast.makeText(this,"costumes unchecked",Toast.LENGTH_LONG).show();

                }
                break;
            case R.id.checkbox_toy:
                if (checked){
                    Toast.makeText(this,"Checked toys",Toast.LENGTH_LONG).show();

                }

            else{
                    Toast.makeText(this,"Toy unchecked",Toast.LENGTH_LONG).show();

                }
                break;

        }
    }


}