android clear radiogroup onclick button(clear)

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.

4 answers

  • answered 2018-07-11 03:59 Shiva Snape

    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();
                }
            });
    

  • answered 2018-07-11 04:15 Amir Hossein Mirzaei

    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);
                            }
    

  • answered 2018-07-11 05:48 Vishal sharma

    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();
    }
    

  • answered 2018-07-11 09:25 I_A_Mok

    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!