1

i've seen this question added on the stack but the information hasn't been helpful or successful yet, so i remain quite not sure.

The gist of what I'm trying to do:
I have a layout defined in xml, some_details_view.xml for example.

setContentView(R.layout.some_details_view);

It has a bunch of text views laid out using a parent relative layout, a linear header layout, a linear footer layout, a middle scroll layout that contains a relative layout that hold some label - value type of text views. And at the bottom of the scroll view relative layout, I currently placed a frame layout as a place holder.

On create of the corresponding activity, I set text in respective text views with data handed over from previous activity; basic stuff.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <LinearLayout
        android:id="@+id/header"

        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

                        ...some header content

    </LinearLayout>         

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >

        ..some footer content

    </LinearLayout>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header"
        android:layout_margin="5dip" >


        <RelativeLayout
            android:layout_width="fill_parent"
            android:id="@+id/relativeScroll"
            android:layout_height="wrap_content" >

            ...text views in relative layout

            <FrameLayout
                android:id="@+id/placeholder"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/moreInfoValue" />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

After setting up text views for the given data, I use an async task to get some additional data that I want to show as a history list type of thing at the bottom of the static form layout. There could be 0 or more items so I either add 1 or more text views or none at all.

In the post execute, which I understand to run on the main UI thread, I try to find an exiting container Layout/view group and add either a new Linear Layout to which I add new text Views, or just add the new text views directly to the existing container layout.

here's the latest thing I tried:

ViewGroup mContainer = null; //defined as member variable of activity class and instatiated in on create
mContainer = (ViewGroup)findViewById(R.id.placeholder); //set in on create
LinearLayout ll = new LinearLayout(context); //on post execute of async task
ll.setOrientation(LinearLayout.VERTICAL);
mContainer.addView(ll); //add a new linear layout to an existing container layout
//add some new text view widgets items dynamically
for(NewDisplayItem item : items)
{
    TextView tv = new TextView(context);
    tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    tv.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setText(item.getSomeText());
    ll.addView(tv); //add text view to new linear layout
}

When the UI loads I don't see new items added to my layout after stepping through and watching the controls get added using the code above.

Not sure what it is but something doesn't seem right about this approach in addition to the fact that it's not working. When the activity loads up, all the static views are setup and in view. I pop up a loading dialog, step through the async task stuff and I guess expect to see the dynamic controls add to the layout one by one?

1
  • I suppose there's a way I can inflate a new layout defined in xml for each new DisplayItem that I want to add to my existing layout.... something like this, stackoverflow.com/questions/7977071/… Commented Aug 31, 2012 at 13:55

2 Answers 2

2

First of all textView.setWidth(int) takes as parameter the width in pixels.

Second you should also set your layout parameters on the LinearLayout you are adding.

The way you should set LayoutParams is as follows :

ll.setLayoutParams(new LinearLayout.LayoutParams(
       LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

the same for yout TextViews.

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

Comments

1

Ovidiu Latcu has a good answer. A good question is: is there a reason why you aren't using a ListView (which btw there ARE cases where what he's doing works better)? A ListView has a lot of mechanisms to help you keep from running out of RAM

2 Comments

well, i'm still fairly green when it comes to android. My understanding is I can't have a ListView inside a scroll view.... I want my form layout to be scrollable, then the history type list could be a list view inside the scroll view but it's not allowed. or are you saying the entire relative text layout could be a list view... where the text view contents can be a row that's inflated, then in post execute, i could inflate a different layout for the items i want to render there and just add them to that same ListView?
come to think of it, i'd rather be inflating a specific layout for each of these dynamic content items instead of trying to layout each of the dynamic items details in code. it's be easier to just set text of text views already laid out the way i want

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.