not able to unchecked the already checked radio button in dynamic radio group

how to check and uncheck single radio button in android
how to make radio button in android studio

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling .setChecked(false); then, later even if I try to make it selected by calling setChecked(true) will not unselect the previous one.

    private void radiotype() {
    count = //changed every time.
    LinearLayout llques = (LinearLayout) mview.findViewById(R.id.llrbgRBD);
    RadioGroup group = new RadioGroup(context);
    group.setOrientation(RadioGroup.VERTICAL);
    final RadioButton[] rb = new RadioButton[count];
    List<String[]> ans = getAnswerList.getAns();

    for (int j = 0; j < count; j++) {

        rb[j] = new RadioButton(context);
        rb[j].setVisibility(View.VISIBLE);
        rb[j].setText("`enter code here`hi");
        String a = rb[j].getText().toString();`enter code here`
        Log.e("getAnswerList===a", "getAnswerList===>a" + a);
        Log.e("getAnswerList", "getAnswerList===>" + ans.get(index)[0]);
        if (a.equalsIgnoreCase(ans.get(index)[0])) {
            rb[j].setChecked(true);
        }
        rb[j].setTextColor(Color.BLACK);
        rb[j].setButtonDrawable(R.drawable.custom_radio_button);
        group.addView(rb[j]);
    }
    llques.removeAllViews();
    llques.addView(group);
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            //int c = count;
            for(int i=0;i<count;i++){
                rb[i].setChecked(false);
            }
            //
            Log.v("id",""+checkedId);
            for (int i = 0; i < count; i++) {
                if (rb[i].getId() == checkedId){
                    rb[i].setChecked(true);
                } 
            }
        }
    });

I know this is rather old, but I just had the same problem. The solution is to change the state of RadioButton after adding it to RadioGroup by addView(). I guess this is also what BAKUS tried to say by his sample code.

How to make a radio button unchecked by clicking it?, llrbgRBD); RadioGroup group = new RadioGroup(context); group.setOrientation(​RadioGroup.VERTICAL); final RadioButton[] rb = new RadioButton[count];  In android, Radio Button is a two-states button that can be either checked or unchecked and it’s the same as CheckBox control, except that it will allow only one option to select from the group of options. The user can press or click on the radio button to make it select. In android, CheckBox control allow users to change the state of control either Checked or Unchecked but the radio button cannot be unchecked once it is checked.

The way RadioGroup ensure only one RadioButton is checked is based on those RadioButton's Id. Try add this in your first for-loop:

int uniqueId = j; // or whatever unique in this RadioGroup
rb[j].setId(uniqueId);

How can I check whether a radio button is selected with JavaScript , in JavaScript, simply by setting its checked property to false, e.g. In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added in it will automatically unchecked all the others. It means at one time we can checked only one radio button from a group of radio buttons which belong to same radio group.

Use this in your xml.

<RadioGroup
                android:id="@+id/status_group"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:orientation="vertical"
                 android:layout_margin="5dp"
                 >
             <RadioButton
                 android:id="@+id/open_radio"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Open"
                 />
            <RadioButton
                android:id="@+id/close_radio"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Closed"
                 />
            <RadioButton
                android:id="@+id/all_radio"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Both"
                 />

            </RadioGroup>

And then in java:

 final RadioGroup status_group = (RadioGroup) findViewById(R.id.status_group);

        //--    By default if you want open button to be checked, you can do that by using
            status_group.check(R.id.open_radio);

            status_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 


                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    status_group.check(checkedId);

                    radio_status = radioButton.getText().toString().trim();
                    Log.v("radio_text--", radio_status);
                }
            });

What is the function of radio button, How do you check radio button is checked or not? Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group. Once the radio group is created, selecting any radio button in that group automatically deselects any other selected radio button in the same group.

public void getqlist(int comp) 
{   
    LinearLayout ll = (LinearLayout) findViewById(R.id.variable);
    ll.removeAllViews();
    xfiche=(ficheflag)?(fiche+1):fichetempo;
    String titre="Formulaire "+xfiche+"- Question "+(comp+1)+"/"+taille;
    TextView titreView =(TextView) findViewById(R.id.titre);
    titreView.setText(titre);    
    int ii= Integer.parseInt(quest[comp][3].toString());
    RadioButton[] rb = new RadioButton[ii];
    RadioGroup rg = new RadioGroup(this); 
    rg.setOrientation(RadioGroup.VERTICAL);
    int isel=Integer.parseInt(resp[comp][2].toString())-1;
    int bid=254211; // You give any integer ID
    for(int i=0; i<ii; i++){
        rb[i]  = new RadioButton(this);
        rb[i].setText(quest[comp][i+5].toString());
        if (i==isel)
            rb[i].setId(bid); // You capure the previously selected radio button
        rg.addView(rb[i]);
    }      
    ll.addView(rg);//you add the whole RadioGroup to the layout
    RadioButton tb=(RadioButton) findViewById(bid); // find the button by the Id 
    tb.toggle();   // toggle it after diplaying the group    

}

Dynamic RadioButton in Kotlin, is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options. If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling .setChecked(false); then, later even if I try to make it selected by calling setChecked(true) will not unselect the previous one. private void radiotype() { count = //changed every time.

Do this -

if (a.equalsIgnoreCase(ans.get(index)[0])) {
            rb[j].setChecked(true);
        }

after

group.addView(rb[j]);

instead of checking it before addView.

Make it like -

private void radiotype() {
count = //changed every time.
LinearLayout llques = (LinearLayout) mview.findViewById(R.id.llrbgRBD);
RadioGroup group = new RadioGroup(context);
group.setOrientation(RadioGroup.VERTICAL);
final RadioButton[] rb = new RadioButton[count];
List<String[]> ans = getAnswerList.getAns();

for (int j = 0; j < count; j++) {

    rb[j] = new RadioButton(context);
    rb[j].setVisibility(View.VISIBLE);
    rb[j].setText("`enter code here`hi");
    String a = rb[j].getText().toString();`enter code here`
    Log.e("getAnswerList===a", "getAnswerList===>a" + a);
    Log.e("getAnswerList", "getAnswerList===>" + ans.get(index)[0]);

    rb[j].setTextColor(Color.BLACK);
    rb[j].setButtonDrawable(R.drawable.custom_radio_button);
    group.addView(rb[j]);
    if (a.equalsIgnoreCase(ans.get(index)[0])) {
        rb[j].setChecked(true);
    }
}
llques.removeAllViews();
llques.addView(group);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
        //int c = count;
        for(int i=0;i<count;i++){
            rb[i].setChecked(false);
        }
        //
        Log.v("id",""+checkedId);
        for (int i = 0; i < count; i++) {
            if (rb[i].getId() == checkedId){
                rb[i].setChecked(true);
            } 
        }
    }
});

This worked for me too.

Android RadioButton with Examples, as Checkbox except that radio button can not allow to be unchecked once it was selected. By default, the RadioButton in OFF(Unchecked) state but we can change the default state of RadioButton by using android:checked attribute. First of all define the RadioGroup in the Linearlayout and access into the Kotlin file. It means if we do not select any radio button or checkbox is not checked then form will not be valid. In reactive form Validators.requiredTrue validates a control for true value and we commonly use it for required checkboxes.

JavaScript to Get Value of Selected Radio Button, In android, we use radio buttons with in a RadioGroup to combine multiple By default, the android RadioButton will be in OFF (Unchecked) state. We can change the default state of RadioButton by using android:checked attribute. Following is the example of creating a RadioButton control dynamically in activity file. I would expect the corresponding radio button ('Joint') to be unchecked. However, the 'Joint' radio button remains checked, which does not represent the true state of the control. As a workaround, I'm able to bind to the md-radio-checked class on each button, which removes the checked state of a radio button when the corresponding binding

Radio button, How to get the value of the selected radio button in a group using JavaScript. the value of the selected radio button, or undefined if no radio button is checked: function getRadioVal(form, name) { var val; // get list of radio buttons with specified name Onchange Event · Check/Uncheck Using JavaScript · Get Value Onclick  So, in activity_main.xml file, we have added two radio buttons inside a radio group. One radio button represents gender male and another represents female. Now, only one radio button can be selected at a time. Now, we will access this widget in kotlin file and show a proper message whenever a radio button is selected. 4.

Why is it impossible to deselect HTML "radio" inputs?, If set to a new radio button, the radio group value will be updated to match the new If there is not such a corresponding radio button, this value persists to be by clicking it if it is currently unchecked, or doing nothing if it is already checked​. Radio buttons Bootstrap radio button. A 'radio button' is a component used to allow a user to make a single choice among a number of options (whereas Checkboxes are used for selecting multiple options).

Comments
  • how to do in dynamic radio button and radio button group
  • You would do well to explain your answer a bit rather than just paste the code.
  • As paqogomez said, can you provide some context to this answer? As it is currently written, it's not very helpful.