In this tutorial, we will learn how to implement a checkbox in an android application.
The Android CheckBox is a two-state button that can be checked or unchecked. Checkboxes can be used in a variety of ways. It can, for example, be used to determine the user’s hobby, activate or deactivate a specific activity, and so on.
When there are multiple options in a list, checkboxes are used to allow the user to select any number of them, including zero, one, or several. In other words, each checkbox in the list is independent of the others, so clicking one does not uncheck the others.
Contents
How to create a custom checkbox in Android
//create a custom xml for checkbox in drawable folder// <? xml version="1.0" encoding="utf-8"?> <selector> <item android:state_checked="true" android:drawable="@drawable/checked" /> <item android:state_checked="false" android:drawable="@drawable/unchecked"/> </selector>
The user can choose one or more things from a list using checkboxes. Each checkbox choice should typically be presented in a vertical list. Each checkbox is controlled separately, and you must register a click listener for each one because a set of checkbox options allows the user to select several items.
Learn Java Programming & get a Certificate
Preview: Checkbox in Android
File: activity_main.xml
–> A TextView, Three CheckBoxes, and a button are included in the activity main.xml file. The user is prompted to select his or her choice of course through TextView.
The user selects their options first, then pushes the Select button. After pressing the Select button, a toast will appear, displaying the choices you’ve chosen.
–> Add the below-given code to your file activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="68dp" android:text="Android" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="28dp" android:text="web" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="28dp" android:text="other" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox1" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="196dp" android:onClick="selected" android:text="Select" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.507" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox2" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:gravity="center_horizontal" android:text="DevelopersDome" android:textAlignment="center" android:textColor="@color/teal_700" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
File: MainActivity.kt
–>Add the below-given code to your file MainActivity.kt
package com.sagar.checkbox import android.os.Bundle import android.view.View import android.widget.CheckBox import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { var checkbox: CheckBox? = null var checkbox1: CheckBox? = null var checkbox2: CheckBox? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) checkbox = findViewById<CheckBox>(R.id.checkBox) checkbox1 = findViewById<CheckBox>(R.id.checkBox1) checkbox2 = findViewById<CheckBox>(R.id.checkBox2) } fun selected(v: View?) { var msg = "" if (checkbox!!.isChecked) msg = "$msg Android " if (checkbox1!!.isChecked) msg = "$msg Web " if (checkbox2!!.isChecked) msg = "$msg other " Toast.makeText( this, "$msg development courses selected. Thanks for contacting us! We will be in touch with you shortly.", Toast.LENGTH_LONG ).show() } }
You may like:
Material Dialogs in Android with Example
https://theudaipurstore.com/maharana-pratap/