1

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...

4
  • Are you sure that all 10 exist? Have you stepped through with the debugger? Commented Mar 5, 2011 at 17:00
  • @Nick Campion - Yeah, the array is full with 10 elements - I did step through it. I load each element the same way, with a textview that reads "Test". I only see one item that says "Test" - and it's backgroud is color #323232 as defined by XML - but it's only 40PX in height, shouldn't #323232 be 400px in height? Commented Mar 5, 2011 at 17:04
  • dude you need to set the orientation of your LinearLayout... Commented Mar 5, 2011 at 17:06
  • @techiServices - Actually, there was only 68 seconds difference. I love Stackoverflow, but there are just too many combative people here - and you just presented a perfect case of this. You basically accused him of stealing your answer. Commented Mar 5, 2011 at 18:13

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

@techiServices if it's being posted while I'm writing and I don't get the notice, yes.
you do get notified if an answer has been posted while you are writing one... you also get the opportunity to view the answer. i don't know what browser you use but it works fine on Firefox.
2

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.