3

I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?

LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
        lLayout = new LinearLayout[8];
        for(int i = 0; i < lLayout.length; i++) {
            lLayout[i] = new LinearLayout(this);
            lLayout[i].setId(i);
            lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            if(i%2 == 0) {
                lLayout[i].setBackgroundColor(Color.GREEN);
            } else {
                lLayout[i].setBackgroundColor(Color.MAGENTA);
            }
            parentLayout.addView(lLayout[i]);
        }
2
  • 1
    Maybe try adding an image to the linearlayouts so they have a size. Maybe they are being added but you cant see them because they are empty? Or a textview with some dummy text..
    – nedaRM
    Mar 7, 2013 at 2:20
  • you're still not adding ListViews, not sure why you didn't accept my edit... Mar 7, 2013 at 2:23

3 Answers 3

2

You need to set LayoutParams, try adding this:

lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
2
  • Thanks, would you know why they're still being displayed next to each other instead of underneath each other? Mar 7, 2013 at 2:33
  • 1
    Make sure your parent LinearLayout is set to Veritcal orientation Mar 7, 2013 at 2:34
0

It looks like you are trying to make a listview yourself using linearlayouts rather than actually using the ListView already supported.

If you are actually trying to do that, you should first give the layouts inside the view a width and height. I'd also put the list in a scrollview in case it overflows the outer layout.

3
  • I have a scrollview in my xml. parentLayout is the LinearLayout inside of the ScrollView Mar 7, 2013 at 2:26
  • Okay, you should be fine if you are just adding 8 inner layouts. But if you have thousands, you'll want to use the ListView. It has some optimizations for dealing with this using the default adapter, and adding Lazy Loading can also help.
    – eski
    Mar 7, 2013 at 2:34
  • Thanks, I'll keep that in mind! I'll actually only have maybe 5-8 at maximum for the real application! Mar 7, 2013 at 2:38
0

Maybe try this

LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
        lLayout = new LinearLayout[8];
        for(int i = 0; i < lLayout.length; i++) {
            lLayout[i] = new LinearLayout(this);
            lLayout[i].setId(i);
            lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            if(i%2 == 0) {
                lLayout[i].setBackgroundColor(Color.GREEN);
            } else {
                lLayout[i].setBackgroundColor(Color.MAGENTA);
            }

            LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

            myLayoutParams.leftMargin = 0;
            myLayoutParams.topMargin = 50 * i;
            myLayoutParams.width = myScreenSize; //e.g. 480
            myLayoutParams.height = 50;

            lLayout[i].setLayoutParams(myLayoutParams);

            parentLayout.addView(lLayout[i]);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.