Home > Back-end >  Checkbox appear and disappear in Android kotlin
Checkbox appear and disappear in Android kotlin

Time:11-07

I am currently stuck on how should i change the current code to show if user selected Not Suit and it will show Movie and Drama checkbox or else if uncheck it disappear movie and drama checkbox and only showing Not Suit checkbox

my current code in Movie.kt

chkBoxNotsuit.setOnCheckedChangeListener { 
if (chkBoxNotsuit.isChecked == true) 
chkBoxMovie.isVisible = !isChecked 
chkBoxDrama.isVisible = !isChecked 
else 
chkBoxMovie.isInvisible = !notChecked 
chkBoxDrama.isInvisible = !notChecked 
displayToast(message)
}

fun displayToast(message:String){
    Toast.makeText(this,message,Toast.LENGTH_LONG).show()
}

CodePudding user response:

You can do this

chkBoxNotsuit.setOnCheckedChangeListener { _, isChecked ->
    chkBoxMovie.visibility = if (isChecked) View.VISIBLE else View.GONE
    chkBoxDrama.visibility = if (isChecked) View.VISIBLE else View.GONE
}

CodePudding user response:

chkBoxNotsuit=findViewById(R.id.chkBoxNotsuit)
linearLayout=findViewById(R.id.linearlayout)
chkBoxNotsuit.setOnCheckedChangeListener { buttonView, isChecked ->
        if(isChecked){
            linearLayout.isVisible=true       // Setting the checkboxes Visible
        }else{
            linearLayout.isVisible=false      // Setting the checkboxes Invisible
            displayToast(message)
            }
    }

}
fun displayToast(message:String){
    Toast.makeText(this,message,Toast.LENGTH_LONG).show()
}

}

In Movie.xml you also have to put android:visibility="invisible" as an attribute to linear layout.

  • Related