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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With