change radioButton status generated by code?

3

I am generating a RadioButton series by code and I have not been able to change the state when a RadioButton loses the status of checked. Currently when you checked one and checked another, both are checked.

what I'm trying to do is not to leave that state with something like this.

the way I'm creating RadioButton is this.

     //Creo un LinearLayaout como contenedor de los radioButton
     rl = (LinearLayout) view.findViewById(R.id.content_radioGroup);

        // Initialize a new RadioGroup
        radioGroup_edad = new RadioGroup(getContext());
        radioGroup_edad.setOrientation(RadioGroup.HORIZONTAL);
        //ciclo para crear los radiobutton
        for(int i=0; i<6; i++) {

            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText("r " + i);
            radioButton.setId(i);

            radioButton.setButtonDrawable(R.color.transparent);
            radioButton.setWidth(100);

            radioButton.setTextSize(16);
            radioButton.setTextColor(getResources().getColor(R.color.textColor));
            radioButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            radioGroup_edad.addView(radioButton);

        }

        // Finally, add the RadioGroup to main layout
        rl.addView(radioGroup_edad);

        radioGroup_edad.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//al activarse este evento obtengo el checkedId al cual le voy a poner el estado de check
               RadioButton radiobutton = (RadioButton) view.findViewById(checkedId);
                radiobutton.setTextSize(24);
                radiobutton.setTextColor(getResources().getColor(R.color.textColorWhite));
            }
        });

Some idea of how I can achieve this.

Thanks for the help.

    
asked by anibalajt 28.03.2017 в 01:14
source

2 answers

1

Try the following method:

private void setupRadioGroup() {
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
for(int i=0; i<5; i++){
    rb[i]  = new RadioButton(this);
    rg.addView(rb[i]); 
    rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit); 
submit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        for(int i = 0; i < 5; i++) { 
            rg.removeView(rb[i]);
        }  
        ll.removeView(submit);
        Questions();
    }
});   }
    
answered by 28.03.2017 в 01:19
0

As an option you can change the default properties to all the radiobutton contained in the RadioGroup except the one you selected:

 radioGroup_edad.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {

            RadioButton radiobutton = (RadioButton) view.findViewById(checkedId);
            //Obtienes los elementos dentro del RadioGroup.
            for (int j = 0; j < radioGroup.getChildCount(); j++) {
              if(j != checkedId){//Change other radiobuttons to default properties
                 radioButton.setTextSize(16);
                 radioButton.setTextColor(getResources().getColor(R.color.textColor));
              }else{ //* Elemento seleccionado.
                 //al activarse este evento obtengo el checkedId al cual le voy a poner el estado de check
                radiobutton.setTextSize(24);
                radiobutton.setTextColor(getResources().getColor(R.color.textColorWhite));
              }

            }
        });

This way when you click on an element the others will return to their default properties.

    
answered by 28.03.2017 в 03:00