Fri. Nov 15th, 2024

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")

        }
     
        
    }


}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *