3

I want to display 6 radio buttons of same radio group. But all the 6 radio buttons remain on same line and goes off the screen. How to display them in two rows (3 radio buttons each)? I tried everything possible for me (I am new to android).

Niraj Burde
  • 736
  • 1
  • 7
  • 16
  • ` I tried everything possible for me` what have you tried? please explain. – ariefbayu Jun 09 '12 at 11:31
  • @silent I put radio buttons in relative layout to try leftOf/rightOf, but buttons disappeared. Because layouts are not allowed inside radiogroup tag. And if i divide radiobuttons in two different radiogroups, they would not be in same radiogroup. Tried to wrap content but it didn't work. – Niraj Burde Jun 09 '12 at 11:43

3 Answers3

3

From searching around, there doesn't appear to be a way of doing it, as RadioGroup uses LinearLayout, which does not wrap. As radio buttons must be direct children to the radio group, you can't add sub-layouts to radio group.

This means you will have to implement this layout behaviour manually. Two possible options are:

  • Create a copy of RadioGroup to extend a different layout, or at least allow you control it dynamically.
  • Implement your own custom layout to replace RadioGroup that extends a layout of your choice, and implements OnClickListener. There's a good example here.
Community
  • 1
  • 1
BDFun
  • 531
  • 2
  • 7
  • Thanks BD for confirming what I had actually concluded to, but somewhere in my mind I had a hope that there must be something I'm missing. Thanks to @silent as well – Niraj Burde Jun 10 '12 at 14:35
1

A simple answer would be to wrap your RadioGroup in a ScrollView so the user could scroll to the off-screen buttons (not real elegant, but not code intensive either).

Barak
  • 16,318
  • 9
  • 52
  • 84
0

Dynamically generated by marginTop and marginLeft, to adjust the need to be in the same line of radiobutton. First, get the width of the screen, set the layoutparams marginLeft for the screen width of half, the height of the marginTop needs to be adjusted according to the specific radiobutton.eg:

holder.question = (TextView) convertView.findViewById(R.id.topic_item_question);
            holder.option = (RadioGroup) convertView.findViewById(R.id.topic_item_option);
            holder.option1 = (RadioButton) convertView.findViewById(R.id.topic_item_option1);
            holder.option2 = (RadioButton) convertView.findViewById(R.id.topic_item_option2);

            //为了能够在一行显示两个radiobutton:获取屏幕的宽度
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();//左侧设置的间距
            int height = DensityDpToPx.dpToPx(context, 24);//处于第二个的高度间距

            LinearLayout.LayoutParams params = (LayoutParams) holder.option2.getLayoutParams();
            params.setMargins(width / 2, -height, 0, 0);
            holder.option2.setLayoutParams(params);

            holder.option3 = (RadioButton) convertView.findViewById(R.id.topic_item_option3);
            holder.option4 = (RadioButton) convertView.findViewById(R.id.topic_item_option4);

            LinearLayout.LayoutParams paramsTwo = (LayoutParams) holder.option4.getLayoutParams();
            paramsTwo.setMargins(width / 2, -height, 0, 0);
            holder.option4.setLayoutParams(paramsTwo);
nihaoqiulinhe
  • 259
  • 3
  • 5