dev90
dev90

Reputation: 7539

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"
    />

Upvotes: 3

Views: 1951

Answers (3)

HiPradeep
HiPradeep

Reputation: 91

full_pay_check.setChecked(!full_pay_check.isChecked());

Upvotes: 0

Sukrit Kumar
Sukrit Kumar

Reputation: 386

The best way to deal with this would be to use a Checkbox widget provided as a RadioButton isn't ideal in this situation. Still this is quite achievable. Preetika Kaur's code is fine but would uncheck the RadioButton as soon as i enable it. However there is an easy work around this too. Here is an example using Butterknife.

    int i = 0;
    @BindView(R.id.btn) RadioButton btn;

    @OnClick(R.id.btn)
    public void go(RadioButton btn) {
      if(i == 0){
        i++;
        btn.setChecked(true);
      }else{
        i = 0;
        if(btn.isChecked()){
           btn.setChecked(false);
        }else{
          // Not Checked 
        }
      }
    }


/* 
  Other Code
*/

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Make App
  }

Upvotes: 1

Preetika Kaur
Preetika Kaur

Reputation: 2031

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

Upvotes: 1

Related Questions