Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID : split the screen in 2 equals parts with 2 listviews

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

like image 342
Hubert Avatar asked Oct 19 '10 12:10

Hubert


2 Answers

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>
like image 158
Hubert Avatar answered Nov 05 '22 12:11

Hubert


<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

like image 3
Muhammad Zeshan Avatar answered Nov 05 '22 13:11

Muhammad Zeshan