Radio Button goes wrong in android

93 Views Asked by At

Sometimes when i want to select choices in radio buttons there is something wrong with radio buttons which i can select Two radios However sometimes is right as default way it must be worked.

Here two sample in my application : sample1sample2

Here's my Code :

 span.replace(0,0,getText(R.string.ForeSingleChoice));
                    new DynamicViews().makeNormalContent(getApplicationContext(),span,linearLayout,16,16,16,16,16.0f);
                    span.clear();

                    radioGroup = new DynamicViews().makeRadioGroup(getApplicationContext(),linearLayout);
                    new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),-1);
                    new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5);
                    new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),-1);
                    new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),-1);

                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId)
                            {
                                case 5: goToNextLevel= true ;
                                    break;
                                default: goToNextLevel = false;
                                    break;
                            }
                        }
                    });

DynamicViews makeRadioButton:

public RadioButton makeRadioButton(Context context, RadioGroup radioGroup, String text, int Id) {
    RadioButton radioButton = new RadioButton(context);
    LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    radioButton.setBackgroundResource(R.drawable.backgroundbetweenradios);
    radioButton.setLayoutParams(Params);
    radioButton.setPadding(dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context));
    radioButton.setId(Id);
    radioButton.setText(text);
    radioButton.setTextColor(Color.BLACK);
    radioGroup.addView(radioButton);

    return radioButton;
}

DynamicViews makeRadioGroup:

public RadioGroup makeRadioGroup(Context context, LinearLayout linearLayout) {
    RadioGroup radioGroup = new RadioGroup(context);
    LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    radioGroup.setLayoutParams(Params);
    radioGroup.setOrientation(LinearLayout.VERTICAL);

    CardView cardView = new CardView(context);
    cardView.setCardBackgroundColor(Color.WHITE);
    cardView.setCardElevation(8.0f);
    Params.setMargins(dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context));
    cardView.setLayoutParams(Params);
    cardView.addView(radioGroup);
    linearLayout.addView(cardView);

    return radioGroup;
}

My application is consists of a lot of radiobuttons ,This is the approach i've made these radio buttons , what's the problem ? is this a bug ?

2

There are 2 best solutions below

3
On BEST ANSWER

Have you tried to give each radio button unique id?

new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),1);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),2);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),3);
2
On

add this on your onCreate()

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    rbtn1.setChecked(yesrbtn == buttonView);
                    rbtn2.setChecked(norbtn == buttonView);
                    ....

                }

            }

        };

    rbtn1.setOnCheckedChangeListener(listener);
    rbtn2.setOnCheckedChangeListener(listener);

what this does is making sure that radiobuttons are not allowed to have 2 or more selection