Kirmani88 Kirmani88 - 2 months ago 15
Android Question

How to deal with Single Radio button on a Layout

I have a scenario where i have a single Radio button on screen. I want it to act like a checkbox, so a user can select or deselect that radio button. Right now if user clicks on radio button, there is no way he can un-check that radio button.

Kindly guide me how to uncheck radio button.

Radio Button XML

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Remember me"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:fontFamily="sans-serif-light"
android:layout_weight="1"
style="@style/radionbutton"
android:paddingLeft="10dp"
android:textColor="@color/grayCheckoutFont"
/>

Answer
RadioButton rb;

rb = (RadioButton) findViewById(R.id.rb);


if(rb.isChecked())
{
  \\ is checked  set it unchecked here
  rb.setChecked(false);
}
else
{
  \\ not checked
}

Try it