Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two ListView Side By Side

I have two ListView. And I need to place them side by side horizontally. But the problem is - Only one list is visible. (Note : The second list is in right side of the layout and can have at most 1 character. And the first list will expand to fill the rest of the screen.)

Help me please.

The layout is like this.

------------
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
------------

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

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

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

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="true" />
</LinearLayout>

Shaiful

like image 707
Shaiful Avatar asked Feb 09 '11 10:02

Shaiful


2 Answers

I can't believe nobody has come up with this in three years. Perhaps somebody else will have this question.

The issue is that ListView is greedy when it comes to horizontal space. You can tell it to pipe down using layout_width='0dp' and whichever layout_weight numbers you feel are best. Below is an example putting leftListView at ¾ and rightListView at ¼. I tried it, it works fine.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ListView
        android:id="@+id/leftListView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight=".75" />

    <ListView
        android:id="@+id/rightListView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight=".25" />

</LinearLayout>

Another alternative is to so the same layout trick but with FrameLayouts, then create fragments for each ListView. It's heavier but you might find value in having the Fragments lying around.

like image 88
QED Avatar answered Nov 02 '22 22:11

QED


I think use this code it will work fine. You have to use layout_weight = 0.5 and things will work perfectly fine.

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginTop="60dp"
android:orientation="horizontal"
android:id="@+id/linearLayout2">
    <ListView
        android:id="@+id/sportsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sports_array"
        android:layout_weight="0.5"/>

    <ListView
        android:id="@+id/sportsList_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sports_array"
        android:layout_weight="0.5"/>
</LinearLayout>

So, basically use two list views in a linear layout and give the weight of each layout to 0.5 . I think this helps.

like image 38
Samyak Upadhyay Avatar answered Nov 02 '22 23:11

Samyak Upadhyay