-1

I added two ListViews in a layout. I want them to maintain their 'layout_weight's throughout the activity. But when I add items to the second ListView, first one shrinks ignoring the layout_weight assigned. How can I solve this issue?

Here's what I expect :

And here's the code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_add_group_members_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/content_add_group_text_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="First List" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#DDD" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <TextView
                android:id="@+id/added_people_empty_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="List One Empty View"/>

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Second List" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#DDD" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/list_contacts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false" />

            <TextView
                android:id="@+id/contacts_empty_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="List Two Empty View"/>

            <com.github.silvestrpredko.dotprogressbar.DotProgressBar
                android:id="@+id/dots"
                android:layout_width="wrap_content"
                android:layout_height="16dp"
                android:layout_gravity="center"
                android:visibility="gone" />

        </FrameLayout>

    </LinearLayout>

</LinearLayout>
aruke
  • 611
  • 1
  • 10
  • 17
  • 1
    The `layout_height` on the root `LinearLayout` should be `match_parent`. That also goes for both `FrameLayout`s and both `ListView`s. – Mike M. Aug 22 '16 at 12:58
  • That solves it @MikeM !! Can you explain what was the problem? – aruke Aug 22 '16 at 14:20
  • You never wanna use `wrap_content` for the height of a `ListView`, but the main problem was probably the `wrap_content` for the root `LinearLayout`'s height. To wrap, it needs to know the heights of its children, but when figuring the children's heights with weights, it needs to know its own height. That circular dependency, along with the inexact height on the first group's children, was leading to the unexpected behavior. – Mike M. Aug 22 '16 at 14:44

1 Answers1

1

Try the below code Add android:weightSum="2" in your parent layout and set android:layout_weight="1" to its both childs for equal spaces.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_add_group_members_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/content_add_group_text_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="First List" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#DDD" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <TextView
                android:id="@+id/added_people_empty_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="List One Empty View" />

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Second List" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#DDD" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/list_contacts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false" />

            <TextView
                android:id="@+id/contacts_empty_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="List Two Empty View" />

            <com.github.silvestrpredko.dotprogressbar.DotProgressBar
                android:id="@+id/dots"
                android:layout_width="wrap_content"
                android:layout_height="16dp"
                android:layout_gravity="center"
                android:visibility="gone" />

        </FrameLayout>

    </LinearLayout>

</LinearLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41