info@androidpaper.co.in

How To Use CheckBox In Android Studio

An Android Checkbox is a user interface component that allows users to make multiple selections from a list of options. It is a type of widget or view that represents a binary choice, where users can either select or deselect an option.

A Checkbox in Android displays a small square box that can be checked or unchecked by the user. When the checkbox is checked, a small checkmark or tick mark appears inside the box, indicating that the option is selected. Conversely, when the checkbox is unchecked, the box remains empty.
xml design

<?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:orientation="vertical"
    tools:context=".CheckboxActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/teal_700">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Check Box"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="Menu Selection"
            android:textSize="25dp" />

        <CheckBox
            android:id="@+id/chRas"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rasgulla"
            android:textSize="15sp" />

        <CheckBox
            android:id="@+id/chPantua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pantua"
            android:textSize="15sp" />

        <CheckBox
            android:id="@+id/chKaju"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kaju Katli"
            android:textSize="15sp" />

        <CheckBox
            android:id="@+id/chAmara"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Amarakhand"
            android:textSize="15sp" />

    </LinearLayout>

    <Button
        android:id="@+id/btn"
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="SUBMIT"/>

    <TextView
        android:id="@+id/tv_item"
        android:layout_marginTop="25dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:text="Items : "
        android:textColor="@color/teal_700"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tvtotalcoost"
        android:layout_marginTop="25dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Total Cost : "
        android:layout_marginLeft="10dp"
        android:textColor="@color/teal_700"
        android:textSize="25dp" />

</LinearLayout>

In MainActivity.java

 Button btn;
    CheckBox chAmara,chKaju,chPantua,chRas;
    TextView tvtotalcoost,tv_item;
    int totalAmount=0;
    String itemString="";

onCreate()

 tvtotalcoost = findViewById(R.id.tvtotalcoost);
        tv_item =findViewById(R.id.tv_item);
        chRas = findViewById(R.id.chRas);
        chPantua = findViewById(R.id.chPantua);
        chKaju = findViewById(R.id.chKaju);
        chAmara = findViewById(R.id.chAmara);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(chRas.isChecked()){
                    totalAmount = totalAmount+25;
                    itemString = itemString+chRas.getText().toString();
                  //  tv_item.setText("Items :" + chRas.getText().toString());
                }
                if(chPantua.isChecked()){
                    totalAmount = totalAmount+35;
                    itemString = itemString+" "+chPantua.getText().toString();
                   // tv_item.setText("Items :" + chPantua.getText().toString());
                }
                if(chKaju.isChecked()){
                    totalAmount = totalAmount+55;
                    itemString = itemString+" "+chKaju.getText().toString();
                   // tv_item.setText("Items :" + chKaju.getText().toString());
                }
                if(chAmara.isChecked()){
                    totalAmount = totalAmount+58;
                    itemString = itemString+" "+chAmara.getText().toString();
                    //tv_item.setText("Items :" + chAmara.getText().toString());
                }
                tv_item.setText("Items :" +itemString);
                tvtotalcoost.setText("Total Cost : "+totalAmount);
            }
        });

Now, Run and enjoy the code.