'Hides the view when the button is clicked
I have a radio group used to set data class properties. Here's how I've gone so far: I manually set the "bankDestination" view to invisible if proxy_bi_fast is checked. However the "bankDestination" view is still visible.
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
val checked = view.isChecked
when (view.getId()){
R.id.proxy_BI_Fast ->
if (checked) {
binding.bankDestination.visibility = View.GONE
}
}
}
}
the related xml code:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/account_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="@string/account_number"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/proxy_BI_Fast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="20dp"
android:text="@string/proxy_bi_fast" />
</RadioGroup>
Solution 1:[1]
You haven't added onClick method in your proxy_BI_Fast radio button. Try adding that and then check.
Solution 2:[2]
It wont' work android:onClick="onRadioButtonClicked" in the RadioButton instead of this you have to use setOnCheckedChangeListener on the RadioGroup as describe below.
binding.rbGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.proxy_BI_Fast -> {
binding.bankDestination.visibility = View.GONE
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Khar Varsha |
| Solution 2 | Suraj Bahadur |
