0

How to uncheck all the radiobuttons in radiogroup onclick of a button. What are the possible codes. Tried radiogroup.clearcheck() - Not working. My radiogroup is in listview. calling button and listview from two different layout.

On button(clear) click radiobutton should be unchecked

To view code please follow the post repeated post Thank in advance.

contact dummy
  • 571
  • 9
  • 25
  • Possible duplicate of [uncheck radiobutton/radiogroup on Button click - Android](https://stackoverflow.com/questions/51236639/uncheck-radiobutton-radiogroup-on-button-click-android) – Gábor Bakos Jul 11 '18 at 04:55
  • @GáborBakos yes, still looking for the solution. Tried everything. – contact dummy Jul 11 '18 at 04:59

4 Answers4

0

To Clear all the RadioButtons in ListView, In your Adapter Class ,

RadioButton rb;

rb.setChecked(false);

Pass Any Default Value for selected Answer . Say selectedAnser = 0;

in your Adapter ,

if(selectedAnswer.equals("0"))
{
//make all your radiobutton to checked false
radioButton.setChecked(false);
}

In your Activity , while making a clear button click,

 mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              for(int i=0;i<yourDatalist.size();i++)
                  {

                    yourDatalist.get(i).setSelectedAnswer = "0";
                  } 
               //make adapter notify  here
               adapter.notifyDataSetChanged();
            }
        });
Shiva Snape
  • 544
  • 5
  • 9
0

use

 radioGroup.clearCheck();

or

radioGroup.check(-1);

or

             for (int i = 0; i < radioGroup.getChildCount(); i++) {
                            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                            radioButton.setChecked(false);
                        }
Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
  • tried all the solution. can you check my code and tell me where I am going wrong. [link] (https://stackoverflow.com/questions/51236639/uncheck-radiobutton-radiogroup-on-button-click-android) – contact dummy Jul 11 '18 at 05:01
0

use this example

radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioGroup.clearCheck();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            if (null != rb && checkedId > -1) {
                Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
            }

        }
    });

}

public void onClear(View v) {
    /* Clears all selected radio buttons to default */
    radioGroup.clearCheck();
}
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15
  • no luck. onclick button crash. error:07-11 06:01:26.254 10488-10488/com.solution.tracking.forklift E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389) – contact dummy Jul 11 '18 at 06:08
0

Change your CustomAdapter constructor from:

public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("3");
    }
    inflter = (LayoutInflater.from(applicationContext));
}

To:

public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    resetAnswers();
    inflter = (LayoutInflater.from(applicationContext));
}

public void resetAnswers(){
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("3");
    }
}

And inside OnClick() for Clear Button:

customAdapter.resetAnswers();
customAdapter.notifyDataSetChanged();

Hope that helps!

i_A_mok
  • 2,744
  • 2
  • 11
  • 15