0

I have an activity with a listview that gets populated with preferences from an xml file, then another listview that gets populated by data in my database, then a textview and a edittext.

I want every element (including all the list views) on the screen to take their full height, and the entire screen be scrollable, but I cannot figure out how to do this!

Below is my xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:fillViewport="true" >

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

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

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

        <LinearLayout
            android:id="@+id/llPlaces"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textAppearance="?android:attr/textAppearanceLarge" >
            </TextView>

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

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

                <EditText
                    android:id="@+id/txtNewPlaceName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
                </EditText>

                <Button
                    android:id="@+id/btnAddNewPlace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add New" >
                </Button>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

Any ideas why this is not performing as I want and would expect?

Thanks, Max.

Max Mumford
  • 2,482
  • 5
  • 28
  • 40

3 Answers3

0

Well, first off, there is really no reason to encapsulate your ListView inside of a Linear Layout inside of a Linear Layout. In fact, there is no reason to encapsulate any of your views inside two LinearLayouts. Try this and see what happens:

<?xml version="1.0" encoding="utf-8"?>

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

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

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </ListView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

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

        <EditText
            android:id="@+id/txtNewPlaceName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </EditText>

        <Button
            android:id="@+id/btnAddNewPlace"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add New" >
        </Button>

    </LinearLayout>

</ScrollView>
Michael Fox
  • 611
  • 3
  • 9
  • Thanks, Tried the above, the result of which was each element taking up the correct height, however the scrollview was not scrollable for some reason. Scrolling literally does nothing, despite the fact that the EditText and Button are off the screen... Any other ideas? – Max Mumford Dec 01 '11 at 21:51
  • Try setting the android:orientation of the ScrollView to vertical? I had the same issue with a recent app I was developing and I think this resolved the issue. I could be wrong, I will check my source when I can and get back to you. – Michael Fox Dec 01 '11 at 22:02
0

Never put a ListView inside a ScrollView because there is no good way to differentiate between two nested scrollable containers.

gwvatieri
  • 5,133
  • 1
  • 29
  • 29
  • Why? The above kinda works... the second scroll view is scrollable but only on the edges, i.e. suggesting that the dragging gesture isnt captured on listviews – Max Mumford Dec 01 '11 at 21:49
  • If for working you mean that they layout is rendered and it does not crash, yes it works, but as I said, since both ListView and ScrollView are scrollable, you could have weird behaviors since they will compete for scrolling. I just think that the experience could be bad for some users which,for example, have small screen or big fingers, etc... – gwvatieri Dec 01 '11 at 22:00
  • Indeed, the first rule of ScrollView is do not nest ScrollView or ListView – David Snabel-Caunt Dec 02 '11 at 00:07
  • Ok fair enough. By working I mean each element takes full height but the whole screen is not scrollable. Ended up using a seperate listactivity instead. Thanks for the posts – Max Mumford Dec 02 '11 at 17:55
0

Similer problem with solution on this thead:

Android ScrollView layout problem

Unfortunatly it looks like you cannot have a listview in a scrolllayout.

For people who are interested, looks like a potential workaround could be found here either now or in the future:

ListView in ScrollView potential workaround

Community
  • 1
  • 1
Max Mumford
  • 2,482
  • 5
  • 28
  • 40