1
\$\begingroup\$

I want to display multiple radio buttons in a layout with multiline style as an item inside a RecyclerView. Look at the code I provided below and suggest how I can refactor the code to be more efficient or optimized:

Layout in XML:

<RadioButton
    android:id="@+id/rb_rad1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dp_10"
    android:checked="true"
    android:text="@string/text1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/xyz" />


<RadioButton
    android:id="@+id/rb_rad2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp_34"
    android:checked="false"
    android:text="@string/text2"
    app:layout_constraintBaseline_toBaselineOf="@+id/rb_rad1"
    app:layout_constraintLeft_toRightOf="@+id/rb_rad1" />

<RadioButton
    android:id="@+id/rb_rad3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/text3"
    app:layout_constraintLeft_toLeftOf="@+id/rb_rad1"
    app:layout_constraintTop_toBottomOf="@+id/rb_rad1" />

<RadioButton
    android:id="@+id/rb_rad4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/text4"
    app:layout_constraintLeft_toLeftOf="@+id/rb_rad2"
    app:layout_constraintTop_toBottomOf="@+id/rb_rad2" />

Inside the onBindViewHolder() method:

holder.binding.rbRad1.setOnClickListener(object : View.OnClickListener {
                override fun onClick(view: View?) {
                    holder.binding.rbRad2.isChecked = false
                    holder.binding.rbRad4.isChecked = false
                    holder.binding.rbRad3.isChecked = false
                    notifyDataSetChanged()
                }
            })

            holder.binding.rbRad3.setOnClickListener(object : View.OnClickListener {
                override fun onClick(view: View?) {
                    holder.binding.rbRad2.isChecked = false
                    holder.binding.rbRad4.isChecked = false
                    holder.binding.rbRad1.isChecked = false
                    notifyDataSetChanged()
                }
            })

            holder.binding.rbRad2.setOnClickListener(object : View.OnClickListener {
                override fun onClick(view: View?) {
                    holder.binding.rbRad3.isChecked = false
                    holder.binding.rbRad4.isChecked = false
                    holder.binding.rbRad1.isChecked = false
                    notifyDataSetChanged()
                }
            })

            holder.binding.rbRad4.setOnClickListener(object : View.OnClickListener {
                override fun onClick(view: View?) {
                    holder.binding.rbRad2.isChecked = false
                    holder.binding.rbRad3.isChecked = false
                    holder.binding.rbRad1.isChecked = false
                    notifyDataSetChanged()
                }
            })
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

In your layout XML file, keep the checked value of all RadioButton to false.

<RadioButton
    android:id="@+id/rb_rad1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dp_10"
    android:checked="false"
    android:text="@string/text1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/xyz" />
...

Inside onBindViewHolder() method, pass the clicked RadioButton to another method customRadioButtonOnClick where you'll check what value the passed RadioButton holds and based on that, set the checked values of that one or the other buttons to true or false as per your logic like this:

        rb_rad1.setOnClickListener {
            customRadioButtonOnClick(rb_rad1)
        }
        rb_rad2.setOnClickListener {
            customRadioButtonOnClick(rb_rad2)
        }
        rb_rad3.setOnClickListener {
            customRadioButtonOnClick(rb_rad3)
        }
        rb_rad4.setOnClickListener {
            customRadioButtonOnClick(rb_rad4)
        }

customRadioButtonOnClick

private fun customRadioButtonOnClick(radioButton: RadioButton) {
    when (radioButton) {
        rb_rad1 -> rb_rad1.isChecked = true
        rb_rad2 -> rb_rad2.isChecked = true
        rb_rad3 -> rb_rad3.isChecked = true
        rb_rad4 -> rb_rad4.isChecked = true
    }
}  
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.