4

I have a ScrollView and I want to insert a user specified number of HorizontalScrollViews. So what user says he wants to have a matrix of 5x5 elements, I want to insert 5 HorizontalScrollViews with 5 EditText objects each. My program adds the first line just as it's supposed to, but the rest not.

for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(par2);
        HorizontalScrollView row = new HorizontalScrollView(this);
        row.setLayoutParams(par1);
        row.addView(ll);
        for (int j = 0; j < number; j++) {
            EditText txt = new EditText(this);
            txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            txt.setHint(i+","+j);
            ll.addView(txt);
        }
        latout_in_scrollview.addView(row);
    }

Any ideas why? Thanks!

EDIT: The 1:1 code im using

LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_dijkstra);

    dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);

    Bundle extras = getIntent().getExtras();
    number = extras.getInt("vertexes");

    for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(par2);
        HorizontalScrollView row = new HorizontalScrollView(this);
        row.setLayoutParams(par1);
        row.addView(ll);
        for (int j = 0; j < number; j++) {
            EditText txt = new EditText(this);
            txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            txt.setHint(i+","+j);
            ll.addView(txt);
        }
        dijkstra_rows.addView(row);
    }
}
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72

3 Answers3

5

ScrollView can contain only one childView. You can put any layout as per your requirement. I generally use Relative Layout...

Then add views dynamically to relative layout

 viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
        View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);

    // INFLATE YOUR NEW VIEW YOU WANT TO ADD
                CardView cardView = (CardView) 

LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            //Set id to view
            int id = 125;
            if (lastCard != null) {
                params.addRule(RelativeLayout.BELOW, lastCard.getId());
                id = lastCard.getId() + 125;
            }
            cardView.setLayoutParams(params);
            cardView.setId(id);
            viewLayout.addView(cardView);
Pratap Singh
  • 4,607
  • 1
  • 22
  • 24
  • 1
    And if your RelativeLayout (or other layout) is already the single child of your ScrollView in your layout XML file then I discovered the hard way that you must do `scrollView.removeAllViews()` and `questionsContainer.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); scrollView.addView(relativeLayout);` Otherwise the scrolling will not work. – Farrukh Najmi Mar 23 '18 at 14:37
  • @FarrukhNajmi You have the right answer for my problem, I did the same as Pratap, but with LinearLayout. Scrollview wouldn't work, your solution made it work, thank you! – Daniel Silva Aug 07 '19 at 21:41
2

ScrollView is a single element container.

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

You are adding multiple LinearLayouts here

 for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
.
.
}

You should have only one out of this loop. Then add this one to your scrollView, in Loop you can add muliple HorizontolScrollViews to this LinearLayout

Akhil
  • 6,667
  • 4
  • 31
  • 61