Android Checkbox use

  1. Effect
  2. Layout code

<CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#03A9F4"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#03A9F4"
        android:textSize="50sp" />

custom style

<LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox4"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/checkbox_shape"
            android:button="@null"
            android:text="" />

        <TextView
            android:id="@+id/fruit1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
    </LinearLayout>

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox5"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/checkbox_shape"
            android:button="@null"
            android:text="" />

        <TextView
            android:id="@+id/fruit2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
    </LinearLayout>


    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox6"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/checkbox_shape"
            android:button="@null"
            android:text="" />

        <TextView
            android:id="@+id/fruit3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
    </LinearLayout>
    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text =android:textColor"Confirm"
        ="#03A9F4"
        android:textSize="20sp" />
        <TextView
        android:id="@+id/fruit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#673AB7"
        android:textSize="50sp" />

checkbox_shape

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/checkbox_c" android:state_checked="true" />
    <item android:drawable="@mipmap/checkbox_n" />

</selector>

3.java code

package com.jsyj.android_p1.model;

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

import androidx.annotation.Nullable;

import com.cmmboy.toastlib.ToastUtils;
import com.jsyj.android_p1.cmm.BaseActivity;
import com.jsyj.android_p1.databinding.ActivityCheckboxBinding;
import com.jsyj.android_p1.databinding.ActivityRadioBinding;

public class CheckboxActivity extends BaseActivity {

    private ActivityCheckboxBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCheckboxBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initClick();
    }

    private void initClick() {
        binding.checkbox1.setChecked( true ); //default selection 
        binding.checkbox4.setChecked( true ); //default selection
        binding.submit.setOnClickListener(v -> {
            String text = "";
            String text2 = "";
            if (!binding.checkbox1.isChecked() && !binding.checkbox2.isChecked() && !binding.checkbox3.isChecked()) {
                ToastUtils.info(CheckboxActivity. this , "You did not select your hometown" );
            } else {
                if (binding.checkbox1.isChecked()) {
                    text += binding.checkbox1.getText().toString();
                }
                if (binding.checkbox2.isChecked()) {
                    text += binding.checkbox2.getText().toString();
                }
                if (binding.checkbox3.isChecked()) {
                    text += binding.checkbox3.getText().toString();

                }
            }
            binding.value.setText(text);


            if (!binding.checkbox4.isChecked() && !binding.checkbox5.isChecked() && !binding.checkbox6.isChecked()) {
                ToastUtils.info(CheckboxActivity. this , "You did not select fruit" );
            } else {
                if (binding.checkbox4.isChecked()) {
                    text2 += binding.fruit1.getText().toString();
                }
                if (binding.checkbox5.isChecked()) {
                    text2 += binding.fruit2.getText().toString();
                }
                if (binding.checkbox6.isChecked()) {
                    text2 += binding.fruit3.getText().toString();

                }
            }
            binding.fruit.setText(text2);
        });
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *