How to set the fixed position for floating action button above very long listview in Android?

Wai Yan Hein picture Wai Yan Hein · Feb 11, 2016 · Viewed 31.1k times · Source

I am developing an Android app that is mostly working with listview. But I am having a problem using Floating Action Button together with Long ListView. My problem is as below.

When the list view is only has a few item. Floating item can be seen.

This is the screenshot:

enter image description here

As you can see above, Floating Action Button is still can be seen.But when ListView has so many items and become excessive to the screen, Floating Action Button cannot been seen.

This is the screenshot of Long Listview

enter image description here

For the second screen shot, it cannot be scrolled down any more.

What I want to achieve is, I want the floating bottom always at the bottom right of screen above Listview. Like fixed position in HTML and CSS.

I want Floating Action Button always here no matter how tall the Listview is

enter image description here

This is my layout file

<?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">
        <ListView
            android:dividerHeight="@dimen/list_item_divider_height"
            android:padding="@dimen/list_padding"
            android:divider="@color/whitesmoke"
            android:id="@+id/listview_podcast_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>

        <TextView
            android:textSize="17dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:textAlignment="center"
            android:id="@+id/tf_no_records"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/stop_podcast_button"
        android:background="@color/colorPrimary"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"/>
</LinearLayout>

How can I fix my code to achieve it?

Answer

ajantha picture ajantha · Feb 11, 2016

Try using RelativeLayout instead of LinearLayout. And for FloatingActionButton add attribute like this,

    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="end|bottom"