How to set a fixed position for a floating action button over a very long list in android?

I am developing an Android application that mainly works with listview . But the problem with the Floating Action Button is related to the Long ListView . My problem is the following.

If the list view has only a few items. Floating item .

This is a screenshot.

enter image description here

As you can see above, the Floating Action Button can still be seen. But when the listview has so many elements and becomes excessive for the screen, the Floating Action Button not visible.

This is a screenshot of Long ListView

enter image description here

For the second screenshot, it can no longer be scrolled down .

What I want to achieve, I want the floating bottom to always be in the lower right corner of the screen above the listview . Like a fixed position in HTML and CSS .

I want a Floating Action Button always here, no matter how high the listview

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> 

So, please, how can I fix my code to achieve it. Help me please. Thanks.

+7
android listview android-fab
source share
3 answers

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

  android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="end|bottom" 
+16
source share

Try using with CoordinatorLayout

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="listview.anubavam.com.listview.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" > </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_menu_camera" /> </android.support.design.widget.CoordinatorLayout> 
+4
source share

Do not place the button in a linear layout.

Use

FrameLayout so you can place views on top of others

0
source share

All Articles