This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Thursday, March 12, 2020

Android kotline disable button when checkbox not checked

Android kotlin disable button when checkbox not checked




In this article today  Android kotlin disable button when checkbox not checked. Checkbox using a checkbox is checked method  using if-else condition follows the code.


    checkBox.setOnClickListener(object:View.OnClickListener() {
      fun onClick(v:View) {
        if (checkBox.isChecked())
        {
          btnDisplay.setEnable(True)
          Toast.makeText(getApplicationContext(), "Button is Visible", Toast.LENGTH_SHORT).show()
        }
        else
        {
          btnDisplay.setEnable(false)
          Toast.makeText(getApplicationContext(), "Button is invisible", Toast.LENGTH_SHORT).show()
        }
      }
    })





1.MainActivity.kt :


import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.CheckBox
import android.widget.Toast
class MyAndroidAppActivity:Activity() {

  private val chkWindows:CheckBox

  private val btnDisplay:Button

  fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)

    btnDisplay = findViewById(R.id.btnDisplay) as Button
    chkWindows = findViewById(R.id.chkWindows) as CheckBox

    checkBox.setOnClickListener(object:View.OnClickListener() {
      fun onClick(v:View) {
        if (checkBox.isChecked())
        {
          btnDisplay.setEnable(True)
          Toast.makeText(getApplicationContext(), "Button is Visible", Toast.LENGTH_SHORT).show()
        }
        else
        {
          btnDisplay.setEnable(false)
          Toast.makeText(getApplicationContext(), "Button is invisible", Toast.LENGTH_SHORT).show()
        }
      }
    })
  }
}



2. activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   
    <CheckBox
        android:id="@+id/chkWindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>


download the source code click image:










  

No comments:

Post a Comment