Android CheckBox Kotlin

15-Sep-2024

Implement CheckBox functionality in Android Studio Project Kotlin

a simple example of using a CheckBox in an Android app. In this example, a CheckBox Checked data shown in TextView:


Here is the tutorial for Java


There Are Some Common Attributes for CheckBox


id
This is a unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
textStyle
bold, italic, normal -> Style Change
background
Set background Color



Step 1 :

File open res/layout/activity_main.xml file −



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mango" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Banana" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Orange" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="Show Result" />
</LinearLayout>



</RelativeLayout>


Step 2 :

File open com.microappvalley.newtestproject/MainActivity.java file −



package com.microappvalley.newtestproject

import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val buttonSubmit: Button =findViewById(R.id.buttonSubmit)
val checkBox1: CheckBox =findViewById(R.id.checkBox1)
val checkBox2: CheckBox =findViewById(R.id.checkBox2)
val checkBox3: CheckBox =findViewById(R.id.checkBox3)
val textResult: TextView =findViewById(R.id.textResult)

buttonSubmit.setOnClickListener {
var checkdData=""
if (checkBox1.isChecked){
checkdData= checkdData+checkBox1.text+"\n"
}

if (checkBox2.isChecked){
checkdData= checkdData+checkBox2.text+"\n"
}

if (checkBox3.isChecked){
checkdData= checkdData+checkBox3.text+"\n"
}
textResult.text=checkdData
Toast.makeText(this@MainActivity,checkdData,Toast.LENGTH_SHORT).show()
}
}
}



Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:


Comments