stephen mc
stephen mc

Reputation: 771

ListView inside LinearLayout breaking out (Android)

Been struggling with this for a while now. I have a layout XML consisting of various LinearLayouts, with separate weighting. The end result looks like this..

Image of Main activity screen

Dont mind the colors, its just to see the break points..Anyway, below the Terminal/Origin etc LinearLayout is a ListView which is populated using a custom adapter. The data loads fine, but then the listView "breaks out" of the LinearLayout, and takes up most of the page

enter image description here

(Dont mind the colors, its just to see the break points..)

ie. it flows over the View above and below.

My XML is as follows:

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

<RelativeLayout
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:background="#FF0000"
    android:gravity="right" >

    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:onClick="getFlightInfo"
        android:text="Refresh" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/table_header"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="0.4"
    android:background="#FFFFFF"
    android:weightSum="1" >

    <TextView
        android:id="@+id/header_cell1"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_1_weight"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="12dp"
        android:text="@string/table_header_terminal"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell2"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_2_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_origin"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell3"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_3_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_flight"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell4"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_4_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_scheduled"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell5"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_5_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_status"
        android:textSize="11dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/table_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:background="#FFFFFF"
    android:orientation="vertical" >

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

</LinearLayout>

<LinearLayout
    android:id="@+id/filter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#00FF00" />

</LinearLayout>

I may be missing something glaring, but for the life of me, I cant see it. Can anyone help?

Thanks a million..

Upvotes: 0

Views: 5118

Answers (4)

Daniel Arg&#252;elles
Daniel Arg&#252;elles

Reputation: 2349

Maybe it is late, but I could help someone.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Content 1" />

</LinearLayout>


<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#00ff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Content 2" />

</LinearLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/linearLayout2"
    android:layout_below="@id/linearLayout1"
    android:background="#00f0f0"></ListView>

Result: Result

Upvotes: 2

Ron
Ron

Reputation: 24235

  • Remove the weight of id/filter.
  • Let the ListView have a weight of 1 with fill_parent height.
  • Remove the listView's immediate parent.

One other thing you missed, is to set the weightSum=sum_of all_child_weight in your root. That should fix the problem.

Upvotes: 0

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

This is because you have kept LinearLayout's height(the one wrapping listview) "wrap_content".Fixing its size to some dip would solve your problem.

Also make listview's height "fill_parent" then.

EDIT :

try this way:

...

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

in upper two layouts,you user 0.2 and 0.4 as weight but in lower two layouts,you users 3 and 1...try it former way and see,if it can help you.also try making height of LinearLayout(the one wrapping listview) to 0dip.

Upvotes: 2

stephen mc
stephen mc

Reputation: 771

Ok.. after consulting this post I came up with a fix

  1. Remove the LinearLayout outside the ListView, its superfluous.
  2. Add a height of '0px' (which I've found around the web) to the ListView ensures it obeys the android:weight parameter properly.

Hope this helps.

Upvotes: 0

Related Questions