Android listview footer doesn't fit properly

I have the following layout influenced by accepted answer here

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >
    ....
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <include
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            layout="@layout/footer" />
        <ListView
            android:id="@+id/song_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/footer"
            android:layout_gravity="center_horizontal"
            android:layoutAnimation="@anim/list_animation"
            android:listSelector="@drawable/bg_list_row_song" />
    </RelativeLayout>
</LinearLayout>

and footer.xml

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

        <ImageView
            android:id="@+id/img_ad"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/adv" />

</LinearLayout>

which produce this image

enter image description here

My problem is that I want the footer at the end of layout and fill_width and then maitain height to make the image fill real mobile screen width. I can do this by setting a certain list-height but don't want that to support longer screens.

Is there any way to calculate footer width first and maintain its height when its at bottom of layout, then assign remaining height to list? and how?

EDIT

Thanks for your answers, I've figured out best way to do this, I scaled my image to be wider than device screen, and I used

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

    <ImageView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:src="@drawable/adv" />

    <ListView
        android:id="@+id/song_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/footer"
        android:layoutAnimation="@anim/list_animation"
        android:listSelector="@drawable/bg_list_row_song" />
</RelativeLayout>

Upvotes: 0

Views: 1134

Answers (4)

user3240329
user3240329

Reputation: 1

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#EE4566" >

    <!-- add list view here  -->

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="vertical" >

     <!-- add footer here  -->

</LinearLayout>

Upvotes: 0

Anjali Tripathi
Anjali Tripathi

Reputation: 1477

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000" />
   <ImageView 
       android:id="@+id/img1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:background="@drawable/ic_launcher"

       />

</RelativeLayout>

try this,may be it will help you.

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this way
<?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:padding="5dp"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/song_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layoutAnimation="@anim/list_animation"
        android:listSelector="@drawable/bg_list_row_song"/>

    <include
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        layout="@layout/list_item" />

</LinearLayout>

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

    <ImageView
        android:id="@+id/img_ad"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:src="@drawable/adv" />

</LinearLayout>

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

1 - In footer xml, you souldn't match parent size, but wrap its contents.
2 - ListView height should match the remaining parent size.

So, it's just a matter of swapping these two height attributes and it will work perfectly.

Upvotes: 1

Related Questions