Lennart
Lennart

Reputation: 10354

Evenly fit ListViews and a Button into a LinearLayout

I want to fit two Listviews (+two Textviews) and a Button evenly into a LinearLayout. The final result should look something like this:

enter image description here

The Listviews can contain any number of items (from empty to requiring to scroll), however the button must be always visible. A lot of answers here suggest to use layout_weight along with layout_height="0dp" and layout_width="fill_parent", however this results in the Listviews taking up all available space and pushing the button out of the screen. I assume the parameters for the button are wrong, but I can't figure out which of them. At least the TextViews are working, since they are positioned correctly in every case.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"                  
              android:gravity="center"
              android:layout_gravity="center">

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"               
            android:textSize="12sp"
            android:gravity="left"/>

    <ListView
            android:layout_height="0dp"                
            android:layout_weight="1"
            android:layout_width="fill_parent"/>

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                
            android:textSize="12sp"
            android:layout_marginTop="12dp"
            android:gravity="left"/>

    <ListView
            android:layout_height="0dp"               
            android:layout_weight="1"
            android:layout_width="fill_parent"/>

    <Button              
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"/>

</LinearLayout>

I want to avoid to further nest the layout with additional LinearLayouts, unless there is no other way.

Upvotes: 0

Views: 216

Answers (1)

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

Something like this (not tested):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"               
            android:textSize="12sp"/>

    <ListView
            android:layout_height="0dp"                
            android:layout_weight="1"
            android:layout_width="fill_parent"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                
            android:textSize="12sp"
            android:layout_marginTop="12dp"/>

    <ListView
            android:layout_height="0dp"               
            android:layout_weight="1"
            android:layout_width="fill_parent"/>

    <Button              
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_width="wrap_content"/>

</LinearLayout>

Your button needs to have wrap_content height and (I think the reason of your bug) LinearLayout should have fill_parent as the height.

Upvotes: 2

Related Questions