casillas
casillas

Reputation: 16813

Adding a Button at the end of ListView

I want to add a button at the end of a list view. I have the following xml file. The list view appears with its contents, but the button does not show up.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

Upvotes: 1

Views: 2231

Answers (4)

M Moersalin
M Moersalin

Reputation: 290

i have the same problem, and solved. you only need wrap that layout into ScrollView

<ScrollView 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" >

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_transaction_history"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

            </android.support.v7.widget.RecyclerView>

            <Button
                android:id="@+id/btn_history_load_more"
                style="?borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-medium"
                android:paddingBottom="@dimen/margin_big"
                android:paddingTop="@dimen/margin_large"
                android:text="@string/text_btn_load_more"
                android:textColor="#009CDE"
                android:textSize="@dimen/text_size_medium" />
    </LinearLayout>
</ScrollView>

Upvotes: 1

Hussein El Feky
Hussein El Feky

Reputation: 6707

Your button is not shown because your ListView took the whole space hiding your button as you set its layout_height to match_parent. match_parent will take the whole available space. So change it to wrap_content like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_parent"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

wrap_content will wrap your view to the least available height/width without hiding anything or making anything unclear.

Upvotes: 1

d3n13d1
d3n13d1

Reputation: 300

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">

<Button
    android:layout_alignParentBottom="true"
    android:text="Project Download"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/download" />

<ListView
    android:layout_above="@id/download"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myListView" /></RelativeLayout>

Upvotes: 0

heloisasim
heloisasim

Reputation: 5016

The problem is with the attribute android:layout_height="match_parent" in your ListView. match_parent means that the listview will fill all the space. The correct way is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

Upvotes: 3

Related Questions