15

I'm trying to put 2 listviews into my layout. The problem is that I don't know the size of each listview in advance. The first listview could have a few items (0, 1, 2 up to roughly 10) and the second listview could have many items (up to 100).

I tried to set the weight of both listviews at 1 but it did not work:

=> If the first listview has only 1 item and the second one 99, you don't see the first item of listview #1 => it's shrinks so much (relative to listview #2) that you don't see it.

So I'm thinking now to split the screen in 2 equals parts (no matter what/no matter the size of each listview) and put the two listviews in each part. Of course it needs to work on any device ... so how do I capture the device screen size, divide it in two and force the listview size to fit in each half of the screen ?

Has anyone done that already ? Is there another option to show two listviews of different sizes on the same layout (should I use a scrollview in some way ? => when the user is reaching the end of the first listview, the second listview appears => is that possible ??)

Thank you for your help and any suggestion ...

Hubert

Hubert
  • 16,012
  • 18
  • 45
  • 51
  • what i mean in "splitting the screen in 2 equals parts" is one half at the top and one half at the bottom, not left/right. (this is not a valid option for me as the items I have to show are already defined) – Hubert Oct 19 '10 at 12:16
  • I don't know how it will be achieved because a listview in activity, must have an id android:id/list, since it is an identifier, there cannot be two similar ones in the same activiy. Some one more wiser than me will be able to achieve this. – viv Oct 20 '10 at 08:30
  • I've got it working - 2 lisviews within the same Activity (not a ListActivity but a "normal" activity) => take a look at Dow Jones PRO => screen "New 52 weeks HIGH/LOW" ... – Hubert Oct 21 '10 at 07:11

3 Answers3

20

I simply had to "encapsulate" my 2 listviews into 2 separate linearlayouts => these 2 linearlayout have a weight of 1 :

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

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

                </ListView>
    </LinearLayout>

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

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

            </ListView>
</LinearLayout>
Hubert
  • 16,012
  • 18
  • 45
  • 51
  • if you want to see this in action, the app is called DOW JONES for Android, or NASDAQ 100 for Android ... select the button "52 weeks HIGH/LOW" to see this split-screen! cheers, H. – Hubert Oct 20 '10 at 08:18
  • For anyone else stumbling upon this, the ADT plugin for Eclipse (v15) doesn't seem to handle this correctly, but it does work when running on an actual phone (and, I assume, emulators). – Eric Brynsvold Nov 28 '11 at 23:16
3
<LinearLayout
        android:id="@+id/ListView_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1" >

        <RelativeLayout
            android:id="@+id/rl_ListView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5" >

            <ListView
                android:id="@+id/lv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                </ListView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_ListView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5" >

            <ListView
                android:id="@+id/lv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>

        </RelativeLayout>
    </LinearLayout>

Create a parent Linear Layout, define a weightsum and split that into two different relative layouts each of weight equals half of total weightsum because in relative layout things are managed easily and properly .I hope this works for you

Shahid Iqbal
  • 2,059
  • 2
  • 21
  • 29
Muhammad Zeshan
  • 104
  • 3
  • 13
-4

Try to set sizes in soft pixels (device independent ones) - like "50sp" or "100sp" - I'm sure you'll find appropriate one

Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • 3
    This answer is so far from the right track... How does using sp help with all the screen sizes? In fact, there is no way you could actually find the right unit to split the screen in half using this... – Richard Lalancette Apr 17 '12 at 19:20