CheckBox in Android

In this tutorial, we will learn about CheckBox in Android. The CheckBox is a kind of button which only have two states checked or unchecked. The CheckBox can be used in my different ways, for example, to show different options in the app that user can select multiple from those options, In this kind of situation you can use CheckBox.

We will learn how to add CheckBox in Android and how to use different methods related to CheckBox. Firstly we will learn how to add CheckBox widget in XML.

XML Code:

<?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:padding="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <CheckBox
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/owlbuddy"
        android:textSize="20dp"
        android:id="@+id/notifications"
        android:layout_marginBottom="10dp"
        android:text="Notifications"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/colorPrimary"/>

    <CheckBox
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/owlbuddy"
        android:id="@+id/updates"
        android:layout_marginBottom="10dp"
        android:textSize="20dp"
        android:text="Auto Updates"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/colorPrimary"/>

    <CheckBox
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/history"
        android:src="@drawable/owlbuddy"
        android:textSize="20dp"
        android:text="Clear History"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/colorPrimary"/>

</LinearLayout>

Output:

Now we will learn how to perform some action when someone will check these CheckBoxes. Firstly will initialize all these CheckBoxes in our java file with findViewById() method and then we will call to setOnCheckedChangeListener() to these CheckBoxes.

Java Code:

public class MainActivity extends AppCompatActivity {

    CheckBox notifications,updates,history;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notifications=(CheckBox)findViewById(R.id.notifications);
        updates=(CheckBox)findViewById(R.id.updates);
        history=(CheckBox)findViewById(R.id.history);


        notifications.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonifView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Notifications On",Toast.LENGTH_SHORT).show();
            }
        });

        updates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Auto Updates On",Toast.LENGTH_SHORT).show();
            }
        });

        history.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    Toast.makeText(getApplication(),"Clear History On",Toast.LENGTH_SHORT).show();
            }
        });

    }
}

Output:

Spread the love
Scroll to Top