tpow
tpow

Reputation: 7884

Problem building layout programmatically

I am building something like a ListView, but I'm rolling my own because I want to do some custom stuff and have more control than using ArrayAdapters.

So I've defined part of my layout in XML, including one LinerLayout inside a ScrollView. My goal is to bind to that Linearlayout in code, then insert additional RelativeLayouts inside the LinearLayout using no XML, just code.

Here is my XML:

            <ScrollView
               xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/ListScroll"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
             >

                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                       android:layout_width="fill_parent"
                       android:id="@+id/ListHolder"
                       android:layout_height="400px"
                       android:background="#323232"
                     >
                     <!--Here is where I want the RelativeLayouts to go...  -->     
                    </LinearLayout>
            </ScrollView>

Then in code, I'm trying to add RelativeLayouts, each 50px in height, to the LinearLayout, the one above that has a height of 400px.

                //The parent container - is defined above in XML.
                itemContainer = new LinearLayout(context);
                itemContainer = (LinearLayout)findViewById(R.id.ListHolder);
                Layouts = new ArrayList<RelativeLayout>();
                Layouts = LoadWithRelativeLayouts();

                for(RelativeLayout listItem: Layouts){  

                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 40);
                        listItem.setLayoutParams(params);
                        itemContainer.addView(listItem);
                  }

Each one of the layouts in the array has a text view in it that says "Test". When I step through the code, there are 10 elements in the array, and all of the textviews are there, so I would expect to see the 400px LinearLayout filled with 10 Relative layouts one after another, each with 50px height (and fill_parent width) reading "Test" - but all I see is one, as if only one got added, or they are all positioned on top of one another.

Getting screenshot now...

Upvotes: 1

Views: 800

Answers (2)

bigstones
bigstones

Reputation: 15257

When you add something to a layout, you have to use layout params of that kind. So as you're adding to a LinearLayout, you should use LinearLayout.LayoutParams.

Then you'll probably also need to set your LinearLayout orientation to vertical, because right now the items you don't see are all in a row offscreen at the right :)

Upvotes: 3

techi.services
techi.services

Reputation: 8533

Try adding android:orientation="vertical" to the LinearLayout holding the RelativeLayouts.

Upvotes: 2

Related Questions