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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF5722"
android:textSize="30sp"
android:textStyle="bold"
android:textAllCaps="true"
android:padding="10dp"
android:layout_margin="10dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/tv1"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android"
android:textColor="#FF5722"
android:textSize="30sp"
android:textStyle="bold"
android:textAllCaps="true"
android:padding="10dp"
android:layout_margin="10dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/check"
></CheckBox>
</LinearLayout>
MainActivity.kt
package com.androinidan.checkboxkotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.Switch
import android.widget.TextView
import android.widget.ToggleButton
class MainActivity : AppCompatActivity() {
var check: CheckBox?=null
var tv: TextView?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv = findViewById(R.id.tv1)
check = findViewById(R.id.check)
check!!.setOnClickListener{
if(check!!.isChecked)
tv!!.text=("Checked")
else
tv!!.text=("UnChecked")
}
}
}