If I set checkbox state in xml initially, It's working fine but When I set in java or Kotlin dynamically It's not working as I expected.
Like an exmaple when I am setting isChecked = true
It supposed to show check box filled with accent color but It's only showing checking border color with accent only but did not filled the color inside.
See when I set the isChecked It's showing accent color in border only.
If I set checkbox state checked = true
and set isChecked = false
in java It looks like this then
Xml Code of CheckBox
<CheckBox
android:checked="false"
android:id="@+id/check_box"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_padding_8dp"
android:layout_width="wrap_content"
android:text="@string/check_box_title"/>
Kotlin Code
check_box.isChecked = false // Setting Uncheck
check_box.isChecked = true // Setting Checked
To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
You call c1. isSelected = false , intead use c1. isChecked = false .
Typically, you should present each checkbox option in a vertical list. To create each checkbox option, create a CheckBox in your layout. Because a set of checkbox options allows the user to select multiple items, each checkbox is managed separately and you must register a click listener for each one.
I experienced the same with you. I've only tested this in an emulator and one real device, but I found 2 ways to do make it ticked without setting it in xml (for example, you need to fetch something from back-end first to determine whether it has to be ticked from the start or not):
1) Delay command using Handler
Handler().postDelayed({ checkBox?.isChecked = true }, 300)
2) Use toggle()
after setting isChecked
checkBox?.isChecked = true
checkBox?.toggle()
It's as if toggle triggers the UI changes. Problem is, from code above, you'll have your check box ticked, but its isChecked
value will be false
.
Since I have to set enabled
too and somehow it messed up when using toggle()
, I settled with the first way. I really hope someone find a more elegant way to solve this...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With