jellyfish jellyfish - 15 days ago 7
Android Question

Limit height of ListView on Android

I want to show a button under a ListView. Problem is, if the ListView gets extended (items added...), the button is pushed out of the screen.

I tried a LinearLayout with weights (as suggested in Android: why is there no maxHeight for a View?), but either I got the weights wrong or it simply didn't work.

Also, I found somewhere the hint to use a RelativeLayout. The ListView would then be set above the button with the "android:layout_above" param.

Problem with this is that I don't know how to position the button afterwards. In the example I found, the View below the ListView was adjusted using "android:layout_alignParentBottom", but I don't want my button to cling to the bottom of the screen.

Any ideas apart from using setHeight-method and some calculating of the required space?

Thanks for your time,
jellyfish




Edit:
I got a lot of useful answers, thanks to all of you!


  • bigstone's & user639183's solution almost worked perfectly. However, I had to add an extra padding/margin to the bottom of the button, as it still would be pushed half way out of the screen (but then stopped)

  • Adinia's answer with the relative layout only is fine if you want the button fixed to the bottom of the screen. It's not what I intended but still might be useful for others.

  • AngeloS's solution was the one I chose at the end as it just created the effects I wanted. However, I made two minor changes to the LinearLayout around the button:


    • First, as I didn't want to have any absolute values in my layout, I changed android:layout_height="45px" to "wrap_content", which works just fine as well.

    • Second, as I wanted the button to be centered horizontically, which is only supported by vertical LinearLayout, I changed android:orientation="horizontal" to "vertical".



    AngeloS also stated in his initial post that he was not sure if the android:layout_weight="0.1" param in the LinearLayout around the ListView had any effect; I just tried and it actually does! Without, the button gets pushed out of the screen again.


Answer

I had this exact issue and to solve it I created two seperate LinearLayouts to house the ListView and Button respectively. From there I put both in another Linear Layout and set that container's android:orientation to vertical. I also set the weight of the the LinearLayout that housed the ListView to 0.1 but I dont know if that has any effect. From there, you can set the height of the bottom container (that has your button) to whatever height you would like.

EDIT this is what i mean:

<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:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   



                  <ListView android:id="@+id/ListView01" 
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>

The above solution will fix the button the the bottom of the screen.


To have the button float at the bottom of the list, change the height of ListView01 to wrap_content:

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

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   

        <ListView android:id="@+id/ListView01" 
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>