My XML
file has a LinearLayout
containing two ListView
with android:height="wrap_content"
. However, when one of the ListView
has too much contents, it pushes another ListView
and eat up its space. What can I do to make it stops pushing at certain height? I've tried adding layout-weight
or maxHeight
but it doesn't help.
Asked
Active
Viewed 151 times
0

Jeff
- 293
- 4
- 13
-
If you want to use `layout-weight`, try to set height as `match_parent` or 0dp – PriyankVadariya Aug 16 '18 at 11:35
-
@PriyankVadariya interesting...what is the difference between `match_parent` and 0dp? Laptop isn't with me so I can't test this out. It would be great if you could explain. – Jeff Aug 16 '18 at 12:36
3 Answers
0
If you want the same amount for both ListViews
equally divided, just put them in a LinearLayout
.
LinearLayout
will have property weightSum=2
while both ListViews
will have, depending on the LinearLayout orientation property
:
If vertical
-> both ListViews
will have android:height="0dp"
and layout-weight=1
If horizontal
-> both ListViews
will have android:width="0dp"
and layout-weight=1

Alessandro Mautone
- 873
- 7
- 11
-
hi, I want them to expand at their will but stop expanding at some point. `android:height="0dp"` would fix their height respective to weight. – Jeff Aug 17 '18 at 02:01
0
Try this :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/green_bg_duplicate"
android:gravity="center"
android:orientation="vertical"
android:weightSum="2">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1" />
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1" />
</LinearLayout>
0
I solved it by doing it programmatically. It seems XML
doesn't give much control for dynamic height. I added onLayoutChangedListener
to both ListView
to detect height change and add height control logic in the listener.

Jeff
- 293
- 4
- 13