How to programmatically reset list of CheckBoxes in Android

In this android programming source code example, we are going to programmatically reset list of CheckBoxes in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to programmatically reset list of CheckBoxes in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".checkbox.CheckBox13">

    <CheckBox
        android:id="@+id/checkBox15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="146dp"
        android:layout_marginLeft="146dp"
        android:layout_marginTop="229dp"
        android:text="CheckBox"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="CheckBox"
        app:layout_constraintStart_toStartOf="@+id/checkBox15"
        app:layout_constraintTop_toBottomOf="@+id/checkBox15" />

    <Button
        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:text="Reset"
        app:layout_constraintEnd_toEndOf="@+id/checkBox16"
        app:layout_constraintTop_toBottomOf="@+id/checkBox16" />

    <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="105dp"
        android:layout_marginBottom="105dp"
        android:text="Reset Checkbox"
        app:layout_constraintBottom_toTopOf="@+id/checkBox15"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

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

public class CheckBox13 extends AppCompatActivity {

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

        final CheckBox checkBox2 = findViewById(R.id.checkBox15);
        final CheckBox checkBox3 = findViewById(R.id.checkBox16);

        Button resetButton = findViewById(R.id.button14);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Checkbox cleared", Toast.LENGTH_SHORT).show();

                if (checkBox2.isChecked()) {
                    checkBox2.setChecked(false);
                }

                if (checkBox3.isChecked()) {
                    checkBox3.setChecked(false);
                }
            }
        });

    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment