Grid of radio buttons in Android 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:

&lt;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" /&gt;


&lt;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" /&gt;

&lt;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" /&gt;

&lt;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" /&gt;


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()
                }
            })
In your layout XML file, keep the checked value of all RadioButton to false.

&lt;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" /&gt;
...


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 -&gt; rb_rad1.isChecked = true
        rb_rad2 -&gt; rb_rad2.isChecked = true
        rb_rad3 -&gt; rb_rad3.isChecked = true
        rb_rad4 -&gt; rb_rad4.isChecked = true
    }
}