CheckBox - Android Studio - Java

How to use the CheckBox using Android Studio & Java?

Learn how to use the Android CheckBox widget to allow users to select multiple options. This tutorial covers the complete implementation using Android Studio, Java, and XML layouts with practical examples like a color selector.

>> Check For Java

activity_main.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="40dp"
        android:text="Check Box"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Your Experiences:"
        android:textColor="#000"
        android:textStyle="bold" />

    <com.google.android.material.checkbox.MaterialCheckBox
        android:id="@+id/androidCb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Android Developer" />

    <com.google.android.material.checkbox.MaterialCheckBox
        android:id="@+id/iosCb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="iOS Developer" />

    <com.google.android.material.checkbox.MaterialCheckBox
        android:id="@+id/graphicsCb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Graphics Designer" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/confirmBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Confirm" />

    <TextView
        android:id="@+id/resultTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.technifysoft.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.checkbox.MaterialCheckBox;


public class MainActivity extends AppCompatActivity {

    //declare views
    private MaterialCheckBox androidCb, iosCb, graphicsCb;
    private MaterialButton confirmBtn;
    private TextView resultTv;

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

        //init checkbox
        androidCb = findViewById(R.id.androidCb);
        iosCb = findViewById(R.id.iosCb);
        graphicsCb = findViewById(R.id.graphicsCb);
        confirmBtn = findViewById(R.id.confirmBtn);
        resultTv = findViewById(R.id.resultTv);

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputData();
            }
        });

    }

    private void inputData() {
        if (!androidCb.isChecked() && !iosCb.isChecked() && !graphicsCb.isChecked()) {
            Toast.makeText(this, "Please select at least one checkbox.", Toast.LENGTH_SHORT).show();
            return;
        }

        StringBuilder result = new StringBuilder();
        if (androidCb.isChecked()) {
            result.append("\nAndroid Developer");
        }
        if (iosCb.isChecked()) {
            result.append("\niOS Developer");
        }
        if (graphicsCb.isChecked()) {
            result.append("\nGraphics Designer");
        }
        resultTv.setText("You're have selected:\n" + result);
    }
}

Screenshots:

CheckBox | Android Studio | Java

CheckBox | Android Studio | Java

Comments

Popular posts from this blog

Manage External Storage Permission - Android Studio - Kotlin

Picture In Picture - Android Studio - Kotlin

SeekBar with Customization | Android Studio | Java