0

I am fetching data from database. Based on the value, I'm dynamically creating radio buttons. When I try to add radio group to that radio buttons, radio group is not setting up. Bcoz of that I can select many radio buttons at once.

attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values 
are Small, Medium and Large. 

Here is the complete code for RadioButton.

RadioGroup radioGroup = new RadioGroup(mMain);
LinearLayout[] attr_layout = new LinearLayout[optionsList.size()];
                    attr_layout[i] = new LinearLayout(mMain);
                    attr_layout[i].setOrientation(LinearLayout.HORIZONTAL);

                    int attr_size = attributes.size();

                        for (int k = 0; k < attr_size; k++)
                        {
                                String price = String.format(Locale.ENGLISH, AppConstants.DECIMAL_POINTS, Float.parseFloat(attributes.get(k).getAttr_price()));
                                String name_price = attributes.get(k).getAttr_name()
                                        +" ("+ mMain.getString(R.string.currency_code)
                                        +" "+ price +")";

                                if(!multiSelect.equals("1")) // This multiselect value 1 and 0 is coming from database.
                                //Based on these value, app will display checkbox or radio button
                                {
                                    final RadioButton radioButton = new RadioButton(mMain);
                                    radioButton.setText(name_price);
                                    radioButton.setId(i + 6);
                                    radioButton.setTextSize(12);
                                    radioButton.setTag(attributes.get(k));
                                    radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
                                    {
                                        radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                    }
                                    setTextFont(radioButton, "Museo_Slab.otf");

                                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.MATCH_PARENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT,
                                            1f);
                                    //lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom);


                                    RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                                    radioGroup.setLayoutParams(params);
                                    attr_layout[i].addView(radioButton, lp);`         
Anil
  • 1,087
  • 1
  • 11
  • 24
Jacky
  • 298
  • 3
  • 15

3 Answers3

0

Radio group allows to check only one button at once belonging to it (if you are trying to check all buttons of a radio group).

Manishoaham
  • 601
  • 1
  • 5
  • 14
0

Add radioButtons into radio group then add into layouts.

Use the code

radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)

instead of

attr_layout[i].addView(radioButton, lp);` 
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

radioGroup.addView(radioButton, lp); instead of attr_layout[i].addView(radioButton, lp); and then put attr_layout[i].addView(radioGroup); after adding all radioButtons, i.e. outside the for loop.

Nithin
  • 221
  • 3
  • 14
  • I did what you mentioned here. Right now, i can select 2 buttons out of three. Need to make it select only one. – Jacky Jul 28 '17 at 08:38
  • Check if you put all the buttons that you want exclusive of each other, inside the same RadioGroup. You probably have buttons inside different groups, which can be simultaneously selected. – Nithin Jul 28 '17 at 08:48
  • Move your radioGroup.setOnCheckedChangeListener outside the for (int k = 0; k < attr_size; k++) loop, to inside if(!multiSelect.equals("1")). – Nithin Jul 28 '17 at 09:02