Android: Dynamically add views to ScrollView

Tomáš 'Guns Blazing' Frček picture Tomáš 'Guns Blazing' Frček · Mar 31, 2013 · Viewed 11.4k times · Source

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);
    }
}

Answer

Pratap Singh picture Pratap Singh · Mar 22, 2016

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);