ListView with button at the bottom in Activity with fixed header and footer

I am developing an Android Application and I have an Activity that looks like this. I have a header and a footer that have fixed positions, and a ListView with clickable items in the middle. Below the ListView, I have a button.

It is working when I have a few items, but as I add more items, the button begins to disappear, like this.

When I have more items in that list that can be shown in the page, I have a scroll in the list. If I scroll down like this I can see all the items in the list, but the button is hidden.

I have had a lot of problems before trying to add a ListView inside a ScrollView, so I know it doesn't work. As I am new in Android development, perhaps my XML layout files are a bit confusing, but I have a parent LinearLayout with two RelativeLayouts inside, one for the header and one for the middle and footer. The XML is something like this:

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

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:background="@drawable/background"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:text="@string/baseTitle"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:textSize="18dp"
            android:textStyle="bold"/>

        <Button 
            android:id="@+id/buttonHelp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/button_help"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="fill_vertical">

        <LinearLayout   
            android:id="@+id/buttonBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"
            android:background="#bdbdbd"
            android:layout_alignParentBottom="true">

            <RelativeLayout   
                android:id="@+id/bottomButtomBar"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/myGroups"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="@string/myGroups"
                    android:gravity="center"
                    android:textSize="13dp"
                    android:textColor="#000000"
                    android:background="@layout/button_meus_grupos"/>
                <Button 
                    android:id="@+id/createGroups"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_centerInParent="true"
                    android:text="@string/createGroups"
                    android:gravity="center"
                    android:textSize="13dp"
                    android:textColor="#000000"
                    android:background="@layout/button_criar_grupos"/>
                <Button 
                    android:id="@+id/configurations"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:text="@string/configurations"
                    android:gravity="center"
                    android:textSize="13dp"
                    android:textColor="#000000"
                    android:background="@layout/button_configuracoes"/>
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout 
            android:layout_above="@id/buttonBar"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <LinearLayout 
                    android:id="@+id/listContent"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#ffffff" 
                        android:orientation="vertical"
                    >
                        <RelativeLayout
                            android:id="@+id/searchBarLayout"
                            android:layout_width="fill_parent" 
                            android:layout_height="48dp"
                            android:orientation="horizontal"
                            android:background="#ffffff"
                            android:gravity="center_vertical"
                            android:clickable="true"
                        >
                            <TextView
                                android:id="@+id/myGroupsLabel"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/myGroups"
                                android:layout_centerInParent="true"
                                android:textColor="#bdbdbd"
                                android:textSize="16dp"
                                android:textStyle="bold"
                            />
                            <ImageView
                                android:layout_alignParentRight="true"
                                android:layout_centerVertical="true"
                                android:layout_marginRight="15dp"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@drawable/ic_search" />
                        </RelativeLayout>
                        <View android:layout_height="2dip" 
                            android:layout_width="wrap_content" 
                            android:background="#cccccc"  
                        />        
                        <ListView android:id="@+id/listview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#000000"
                            android:cacheColorHint="#000000"
                        />
                        <Button
                            android:id="@+id/buttonCreateGroup"
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="20dp"
                            android:background="@layout/button_confirm"
                            android:layout_gravity="center_horizontal"
                            android:text="@string/createOneGroup"
                            android:textColor="#FFFFFF"
                            android:textSize="22dp"
                            android:layout_marginTop="15dp"
                        />
                    </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Does anyone have any tips on how can I keep the footer fixed, scroll down until the end of the list and then show the button? I want that it looks like the button is the last item at the List.

EDIT: Entire XML posted

Upvotes: 0

Views: 3117

Answers (4)

The only way I could achieve the exact requirements I explained above was doing the following:

-In the XML file with the layout of the list items, I added a button with android:visibility="gone".

-In my custom ListView adapter, I always add an empty object in the last position. I simply add an object with a flag that shows it is the last in the list.

-In the method getView() that I override in the custom Adapter, I check if the object in the current position has the flag indicating that it is the last in the list. If so, I set the button visibility to VISIBLE, otherwise I fill the other components.

If anyone is facing the same problem and needs some sample code from my solution just ask here.

Upvotes: 1

STT
STT

Reputation: 274

Try it in the following manner:

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

    <RelativeLayout                               //Header
       android:layout_alignParentTop="true">     
    </RelativeLayout> 

      <ListView 
         android:layout_below="@+id/header" />

    <RelativeLayout                               //Button(footer)
       android:layout_alignParentBottom="true"
       android:layout_below="@+id/listView" >      
    </RelativeLayout>  

</RelativeLayout>

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Try this:

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:text="baseTitle"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/buttonHelp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/buttonBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/bottomButtomBar"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/myGroups"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="15dp"
                    android:gravity="center"
                    android:text="myGroups"
                    android:textColor="#000000"
                    android:textSize="13dp" />

                <Button
                    android:id="@+id/createGroups"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="createGroups"
                    android:textColor="#000000"
                    android:textSize="13dp" />

                <Button
                    android:id="@+id/configurations"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="15dp"
                    android:gravity="center"
                    android:text="configurations"
                    android:textColor="#000000"
                    android:textSize="13dp" >
                </Button>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/buttonBar"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/listContent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

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

                    <RelativeLayout
                        android:id="@+id/searchBarLayout"
                        android:layout_width="fill_parent"
                        android:layout_height="48dp"
                        android:clickable="true"
                        android:gravity="center_vertical"
                        android:orientation="horizontal" >

                        <TextView
                            android:id="@+id/myGroupsLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:text="myGroups"
                            android:textColor="#bdbdbd"
                            android:textSize="16dp"
                            android:textStyle="bold" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:layout_marginRight="15dp" >
                        </ImageView>
                    </RelativeLayout>

                    <ScrollView
                        android:id="@+id/scroll"
                        android:layout_width="fill_parent"
                        android:layout_height="280dip"
                        android:layout_margin="7dip"
                        android:scrollbars="none" >

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <ListView
                                android:id="@+id/listview"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:cacheColorHint="#000000"
                                android:textColor="#000000" />

                            <Button
                                android:id="@+id/buttonCreateGroup"
                                android:layout_width="200dp"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:layout_marginBottom="20dp"
                                android:layout_marginTop="15dp"
                                android:text="createOneGroup"
                                android:textColor="#FFFFFF"
                                android:textSize="22dp" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

Upvotes: 0

znat
znat

Reputation: 13474

The ListView and the Button should be in a RelativeLayout and in the Button you should set android:layout_below="@id/listview"

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

<ListView
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cacheColorHint="#000000"
    android:textColor="#000000" />

<Button
    android:layout_below="@id/listview"
    android:id="@+id/buttonCreateGroup"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="15dp"
    android:background="@layout/button_confirm"
    android:text="@string/createOneGroup"
    android:textColor="#FFFFFF"
    android:textSize="22dp" />

</RelativeLayout>

Upvotes: 1

Related Questions