Home » Java » java – How to check if any checkbox is selected or not in kotlin?-Exceptionshub

java – How to check if any checkbox is selected or not in kotlin?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:
    submit_btn.setOnClickListener {
                var result = ""
                if(rb_gender.checkedRadioButtonId != -1)
                    {
                        result += " Gender: "
                    }
                    if(rb_male.isChecked) result += " Male \n"
                    if(rb_female.isChecked)  result += "Female \n"

       result += " Language Known:  "

            if(cb_english.isChecked) result+= " English"
            if(cb_hindi.isChecked) result+= " Hindi "
            if(cb_punjabi.isChecked) result+= " Punjabi"

               else   result = " PLEASE SELECT GENDER AND LANGUAGE \n"

            result_textView.text = result
        }
    }
}

//This code is not working on some condition like if I select Male and English then compiler run else part……but that’s not true

How to&Answers:

Try this :

    var flag = 0

        if(cb_english.isChecked) {
                result+= " English"
                flag = 1
        }
        if(cb_hindi.isChecked) {
                result+= " Hindi "
                flag = 1
        }
        if(cb_punjabi.isChecked) {
                result+= " Punjabi"
                flag = 1
        }
        if(flag == 0){   
                result = " PLEASE SELECT LANGUAGE \n"
        }

Answer:

you can use string and check if its value is empty or null that means user did not select any checkbox and if its have value that mean user select a check box simple.