0

in my layout i have 5 listviews next to each other, and a want them all together to match the screen size so that everyone of them has a width of 1/5 of the screens width. match parent doesnt work because then every listview is as big as the screen.

actually i use this:

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:layout_marginTop="25dp">

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView2"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView3"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView4"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView5"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView6"
                    android:layout_gravity="center_horizontal|top"/>

        </LinearLayout>
    </ScrollView>

but that of course doesnt work for different screen sizes.

ntm
  • 731
  • 2
  • 13
  • 37

2 Answers2

1

Don't EVER put ListView inside ScrollView, anyways here is how you do it:

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView2" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView3" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView4" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView5" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView6" />

    </LinearLayout>
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • why not? it works perfecly and it was the only way i found to scroll all listviews at the same time – ntm Aug 14 '13 at 16:20
  • To scroll them together you can see this question: http://stackoverflow.com/questions/12342419/android-scrolling-2-listviews-together – M-Wajeeh Aug 14 '13 at 16:27
  • And it is always good idea to Google if you are trying something unusual: https://www.google.com.pk/search?q=android+scroll+two+listviews&oq=scroll+two+listview&aqs=chrome.1.69i57j0l3j69i62l2.6692j0&sourceid=chrome&ie=UTF-8 – M-Wajeeh Aug 14 '13 at 16:28
  • i've already googled and i also found that but it looked a complicated and i thought it would be easier to use a scrollview. so could somebody explain no WHY thats not a good idea? – ntm Aug 14 '13 at 16:32
  • Its a very long answer, Why not watch this video before doing it http://www.google.com/events/io/2010/sessions/world-of-listview-android.html In this video he explained many other things including putting `ListView` inside `ScrollView`. The short answer is `ListView` has its own scroll mechanism that makes sure to recycle views to avoid going into `OutOfMemory` error. I can see you have your `ListView`'s height equals to 700dp, have you thought what will happen if you `ListView` is longer than 700dp?? – M-Wajeeh Aug 14 '13 at 16:38
  • And what if your `ListView` rows are heavy ? You will eventually get out of memory as all the row `View`s will be initialized and `ListView` wont be able to use recycler. – M-Wajeeh Aug 14 '13 at 16:39
  • my listviews get always filled with exactly 11 entries, that all have a maximum length of 2 lines, so they cant get longer than 700dp – ntm Aug 14 '13 at 16:43
0

Put android:weightSum="5" to your LinearLayout and add android:layout_weight="1" to your each ListView. You might need to set your width to 0.

I also wouldn't recommend putting ListView inside ScrollView.

Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50