2

I am having a radiogroup and added radio buttons dynamically to it. I am using radiogroup.check(id) for selecting the radiobutton inside Radiogroup.

v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
    RadioButton rb = new RadioButton(ctx);
    rb.setId(count);
    rb.setTag(valueStrID.get(position).get(count));
    rb.setText(Str);
    if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
        rbposition = count;
   }
   rb.setTextSize(fontSize);
   ((RadioGroup) v).addView(rb);
   count++;
}

And calling this method for selection

((RadioGroup) v).check(rbposition);

But this is not triggering the Checkedchangelistener. What is the problem with the code?

Anand Asir
  • 309
  • 3
  • 13

3 Answers3

1

Call rb.setChecked(true) to check it.

v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
RadioButton rb = new RadioButton(ctx);
rb.setId(count);
rb.setTag(valueStrID.get(position).get(count));
rb.setText(Str);
rb.setTextSize(fontSize);
((RadioGroup) v).addView(rb);
if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
    rbposition = count;
    rb.setChecked(true);
}


count++;
}
Pr38y
  • 1,565
  • 13
  • 21
  • previously i tried it with setChecked method only. But it gives me problem when i am selecting next radiobutton. It discards the mutual selection property. ie) It shows like both radio buttons are selected. – Anand Asir Oct 06 '16 at 07:59
1

my code is working just check. I think your calling this method before adding the RadioButton to RadioGroup, then it should not work

((RadioGroup) v).check(rbposition);

please check the my code its working perfectly.

public class ThirdActivity extends AppCompatActivity {

        RadioGroup radioGroup;
        ArrayList<String>valueStr=new ArrayList<>();
    LinearLayout ll;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third_activity);
    ll= (LinearLayout) findViewById(R.id.linearLayout);

            valueStr.add("ONE");
            valueStr.add("TWO");
            valueStr.add("THREE");



            radioGroup=new RadioGroup(ThirdActivity.this);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {


                   switch (checkedId){
                       case 0:
                           Toast.makeText(ThirdActivity.this,"RadioButton ONE Selected",Toast.LENGTH_LONG).show();
                           break;
                       case 1:
                           Toast.makeText(ThirdActivity.this,"RadioButton TWO Selected",Toast.LENGTH_LONG).show();
                           break;
                       case 2:
                           Toast.makeText(ThirdActivity.this,"RadioButton THREE Selected",Toast.LENGTH_LONG).show();
                           break;
                   }
                }
            });

            int count = 0;
            for (int i=0;i<valueStr.size();i++) {
                RadioButton rb = new RadioButton(ThirdActivity.this);
                rb.setId(count);
                rb.setTag(valueStr.get(i));
                rb.setText(valueStr.get(i));

                rb.setTextSize(16);

                ((RadioGroup) radioGroup).addView(rb);
                if(count==1){
                    radioGroup.check(count);
                }

                count++;
            }
            ll.addView(radioGroup);
        }
    }
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
  • Actually at first i had it this way only. but later changed it We have to register the listener before the check() method. – Anand Asir Oct 06 '16 at 09:01
0
v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
    RadioButton rb = new RadioButton(ctx);
    rb.setId(count);
    rb.setTag(valueStrID.get(position).get(count));
    rb.setText(Str);
    rb.setTextSize(fontSize);
    ((RadioGroup) v).addView(rb);
    if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
        rbposition = count;
    }
    count++;
}

((RadioGroup) v).setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup rg, int arg1) {
        // TODO Auto-generated method stub                                
    }
});

((RadioGroup) v).check(rbposition);

I am giving the listener before the check() method and it is working now.

Anand Asir
  • 309
  • 3
  • 13