0

I want to uncheck selected radio button on second click (it is a single selection radio button code)

Below is my code:

int row_index = -1;

holder.rbBookOfferText.setOnClickListener(view -> {
        holder.rbBookOfferText.setChecked(true);

        row_index = Integer.parseInt(mDataset.get(position).getTaskId());
        notifyDataSetChanged();

    });

    if (row_index == Integer.parseInt(mDataset.get(position).getTaskId())) {
        holder.rbBookOfferText.setChecked(true);
    } else {
        holder.rbBookOfferText.setChecked(false);
    }

enter image description here

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57

3 Answers3

3

You have to set holder.rbBookOfferText.setChecked(true); programmatically on first click. So, rbBookOfferText not unChecked on first click.

0

I got my answer;

    holder.tvBookOfferText.setOnClickListener(view -> {

            if (mDataset.get(position).getTaskId().equalsIgnoreCase(String.valueOf(row_index))) {
                holder.tvBookOfferText.setChecked(false);
                row_index = -1;
            } else {
                holder.tvBookOfferText.setChecked(true);
                row_index = Integer.parseInt(mDataset.get(position).getTaskId());
                notifyDataSetChanged();
            }

        });

        if (row_index == Integer.parseInt(mDataset.get(position).getTaskId())) {
            holder.tvBookOfferText.setChecked(true);
        } else {
            holder.tvBookOfferText.setChecked(false);
        }
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
-2

To uncheck radio button on second click of same radio button you need to take all the radio buttons under one radio group.

    <?xml version="1.0" encoding="utf-8"?>
    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Place 3 order of Margherita and on 4th order get $20 off"
            android:onClick="onRadioButtonClicked"/>
       <RadioButton android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hourly based order"
            android:onClick="onRadioButtonClicked"/>
      <RadioButton android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Place 3 order of Pugliese and get 4th Pugliese free"
            android:onClick="onRadioButtonClicked"/>
      <RadioButton android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="40% order off on Place order "
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>
Android Geek
  • 596
  • 8
  • 14