0

I am try to fix RadioButton layout:

enter image description here

I help radioButton is fix to like this:

enter image description here

here is my code :

    ScrollView sv = new ScrollView(this);
    ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

        RadioButton[] rb = new RadioButton[10];
        RadioGroup rg = new RadioGroup(this); 
        rg.setOrientation(RadioGroup.HORIZONTAL); 
        for(int j=0; j<((JSONArray) value).length(); j++){
               rb[j]  = new RadioButton(this);
               rb[j].setText("" + ((JSONArray) value).getString(j));
               rb[j].setId(i + 100);
               rg.addView(rb[j]);
        }

      ll.addView(rg);
      this.setContentView(sv);
林姿妤
  • 43
  • 6
  • You shouks customize you radiobuttons - https://stackoverflow.com/questions/19163628/adding-custom-radio-buttons-in-android – Yegorf Apr 09 '21 at 09:25

1 Answers1

0

A LinearLayout is made to display one item next to the other, therefore it's not the best choice here.

The easy way is a Gridlayout, which is specifically made to display items in a grid.

For the case that you have lots of Radiobuttons, you could use a Recyclerview with a GridlayoutManager, too, but i guess that's overkill.

You might also try a ConstraintLayout together with Flow to keep your view-hierarchy shallow and scale well on differently-wide devices.

One final word: Use xml wherever you can instead of inflating views, it makes live so much easier. (Does not apply to compose)

m.reiter
  • 1,796
  • 2
  • 11
  • 31