1

I try to place a ListView under a WebView, to do this I use the folloing xml code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:scrollbars="vertical" >

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:textColor="#FFDEC2" />

        <ListView
            android:id="@+id/comments"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        </ListView>
    </LinearLayout>

</ScrollView>

The code displays the content of the WebView and ListView correctly, but the ListView just has the high of about 1.5 times the ListView Item, so not the entire List is displayed but just the first Items.

I tried several combinations of android:layout_height="fill_parent" and android:layout_height="wrap_content" in the LinearLayout, WebView, ListView and the surrounding ScrollView, none of them worked.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
MWeller
  • 105
  • 1
  • 13
  • Sorry I don't have an answer to your question but one observation...putting scrollable UI elements into a `ScrollView` really isn't a good idea and can result in unpredictable behaviour. Both `WebView` and `ListView` are (or can be) scrollable. – Squonk Nov 19 '11 at 01:32
  • I know that this solution isnt nice at all, but in [this](http://stackoverflow.com/questions/8152071/a-listview-under-a-webview) question i asked for an other way, but i'm happy that i found anything at all. – MWeller Nov 19 '11 at 01:47
  • What is the source of the list? Is it variable or do you have a fixed number of items you want to display? If it's fixed, how many items are there? – Squonk Nov 19 '11 at 02:07
  • i am testing it with a string array but if it works i will replace it with a arraylist, which contains the comments of a forum article, so the number is different every time. – MWeller Nov 19 '11 at 02:09
  • But if it's variable then you're never going to see all the items in one go - that's the point of a `ListView` being scrollable - the adapter can reference 100s or 1000s of items and the `ListView` will show a small (finite) number at any one time but allowing the user to scroll through the total number. If you want all of the items to appear below the `WebView` (within a `ScrollView`) then try dynamically creating and adding item views to the `ScrollView`, e.g., one `TextView` per item. I still think having a `WebView` in a `ScrollView` is a bad idea though. – Squonk Nov 19 '11 at 02:26

1 Answers1

3

You should use layout weights. It's a way to use percentages to define your layout.

In the following example, the ListView uses 60% of the space, and the WebView 40% :

<LinearLayout
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1.0" >

    <WebView
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:textColor="#FFDEC2"
        android:layout_weight=".40" />

    <ListView
        android:id="@+id/comments"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".60" >
    </ListView>
</LinearLayout>

Just change these percentages to whatever you want.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • this isn't working, I tried several values, but I think weight isn't working in a ScrollView. – MWeller Nov 19 '11 at 01:42
  • You can try to set fix heights to your two views. I don't think there's an other way if you want to use a ScrollView. For example you can do it programmatically by getting the height of the screen, and set this as the height for each view. – Dalmas Nov 19 '11 at 01:47
  • But I think you should follow MisterSquonk's advice and forgot this layout. IMO you will have another problem after this one, even if you get what you want... – Dalmas Nov 19 '11 at 01:51
  • I already had a solution, where the upper part of the display is the WebView and the lower part the List, but I want to Display a WebView and at the end of the WebView the List. Both should work with one scrollbar. – MWeller Nov 19 '11 at 01:52