MainActivity.kt
package com.example.checkboxdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
// author : www.raviroza.com
// date : 2-Jan-2023, 9.00 am
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
var ch = mutableSetOf("")
fun setChoices()
{
var t = ""
for(tt in ch)
{
t = t + tt + " "
}
lblChoiceList.text = t.toString()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*chkCricket.setOnClickListener()
{
if(chkCricket.isChecked == true)
{
ch.add(chkCricket.text.toString())
}
else
{
ch.remove(chkCricket.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkFootBall.setOnClickListener()
{
if(chkFootBall.isChecked == true)
{
ch.add(chkFootBall.text.toString())
}
else
{
ch.remove(chkFootBall.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkKabbadi.setOnClickListener()
{
if(chkKabbadi.isChecked == true)
{
ch.add(chkKabbadi.text.toString())
}
else
{
ch.remove(chkKabbadi.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkFootBall.setOnClickListener()
{
lblChoiceList.text = lblChoiceList.text.toString() + chkFootBall.text.toString()
}
chkKabbadi.setOnClickListener()
{
lblChoiceList.text = lblChoiceList.text.toString() + chkKabbadi.text.toString()
}*/
chkCricket.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
chkFootBall.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
chkKabbadi.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
}
}
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"
android:layout_margin="4dp"
android:background="@color/MainColor"
tools:context=".MainActivity" >
<LinearLayout
android:background="@color/SubColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/lblHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#2196F3"
android:gravity="center"
android:text="Check Box Demo"
android:textColor="#232031"
android:textSize="30sp" />
<TextView
android:id="@+id/lblSubHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="Select Your Game"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkCricket"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/cricket"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkKabbadi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/Kabbadi"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkFootBall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/Football"
android:textSize="18sp" />
<TextView
android:id="@+id/lblHereAreYourChoices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:background="#4876a8"
android:text="Your Choices are "
android:textSize="18sp" />
<TextView
android:id="@+id/lblChoiceList"
android:layout_width="match_parent"
android:layout_height="301dp"
android:layout_margin="4dp"
android:gravity="center_horizontal"
android:text=".... "
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Like this:
Like Loading...