Android: Creating a FlowLayout

Sometimes in mobile development you want to create a layout programmatically with an unknown number of elements. You may want to add elements to it dynamically and have the layout height grow as elements are added. Unfortunately, in Android, there is no native FlowLayout. Here is a method that gets us this functionality. I worked on this with Sherif elKhatib on StackOverlow. Actually, he wrote the original version and I’ve optimized it account for a layout which doesn’t fill the entire width of the screen. The code below turns a native LinearLayout into a FlowLayout. Just pass in the LinearLayout, the set of Views you want to add to the LinearLayout, the Context, and any extra view that may be on the left or right of the LinearLayout.

(Note: Make sure your LinearLayout has orientation vertical.)

 * Copyright 2011 Sherif 
 * Updated by Karim Varela to handle LinearLayouts with other views on either side.
 * @param linearLayout
 * @param views : The views to wrap within LinearLayout
 * @param context
 * @param extraView : An extra view that may be to the right or left of your LinearLayout.
 * @author Karim Varela
private void populateViews(LinearLayout linearLayout, View[] views, Context context, View extraView)
    extraView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // kv : May need to replace 'getSherlockActivity()' with 'this' or 'getActivity()'
    Display display = getSherlockActivity().getWindowManager().getDefaultDisplay();
    int maxWidth = display.getWidth() - extraView.getMeasuredWidth() - 20;


    LinearLayout.LayoutParams params;
    LinearLayout newLL = new LinearLayout(context);
    newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    int widthSoFar = 0;

    for (int i = 0; i < views.length; i++)
        LinearLayout LL = new LinearLayout(context);
        LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        LL.setLayoutParams(new ListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        views[i].measure(0, 0);
        params = new LinearLayout.LayoutParams(views[i].getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);

        LL.addView(views[i], params);
        LL.measure(0, 0);
        widthSoFar += views[i].getMeasuredWidth();
        if (widthSoFar >= maxWidth)

            newLL = new LinearLayout(context);
            newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
            newLL.addView(LL, params);
            widthSoFar = LL.getMeasuredWidth();