AppCompatCheckBox

AppCompatCheckBox is a type of two state button either unchecked or checked in Android. Or you can say it is a type of on/off switch that can be toggled by the users. For example if you want to create a form where want to select multiple choice or hobbies than you can use AppCompatCheckBox

Class Hierarchy
public class AppCompatCheckBox extends CheckBox implements TintableCompoundButton, TintableBackgroundView
java.lang.Object
↳ android.view.View
  ↳ android.widget.TextView
    ↳ android.widget.Button
      ↳ android.widget.CompoundButton
        ↳ android.widget.CheckBox
          ↳ androidx.appcompat.widget.AppCompatCheckBox

We Recommend to use new UI components i.e. androidx components


We can create AppCompatCheckBox both in layout.xml or Programatically

Create AppCompatCheckBox in layout xml
<androidx.appcompat.widget.AppCompatCheckBox
    android:id="@+id/choice1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Apple" />


Create AppCompatCheckBox in programatically
AppCompatCheckBox cbx = new AppCompatCheckBox(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
cbx.setLayoutParams(params);

cbx.setText("Apple");


Let's make an program to select choice of a person

layout/checkbox_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Select your fruit choice"
        android:textSize="12pt"
        android:textStyle="bold" />

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/choice1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Apple" />

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/choice2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Orange" />

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/choice3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Mango" />

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/choice4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Papaya" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnShowChoices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:onClick="choices"
        android:text="You choose"/>
</androidx.appcompat.widget.LinearLayoutCompat>

CheckBoxActivity.java
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatCheckBox;

public class CheckBoxActivity extends AppCompatActivity {
    private AppCompatCheckBox choice1;
    private AppCompatCheckBox choice2;
    private AppCompatCheckBox choice3;
    private AppCompatCheckBox choice4;

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

        choice1 = findViewById(R.id.choice1);
        choice2 = findViewById(R.id.choice2);
        choice3 = findViewById(R.id.choice3);
        choice4 = findViewById(R.id.choice4);
    }

    public void choices(View view) {
        StringBuffer choiceBuff = new StringBuffer();
        choiceBuff.append("Your choices are: ");
        if(choice1.isChecked()) {
            choiceBuff.append(choice1.getText().toString() + ", ");
        }
        if(choice2.isChecked()) {
            choiceBuff.append(choice2.getText().toString() + ", ");
        }
        if(choice3.isChecked()) {
            choiceBuff.append(choice3.getText().toString() + ", ");
        }
        if(choice4.isChecked()) {
            choiceBuff.append(choice4.getText().toString() + ", ");
        }

        Toast.makeText(this, choiceBuff.toString(), Toast.LENGTH_SHORT).show();
    }
}