-1

what I want to implement is a screen where I have RecyclerView that shows a list .. it may has many or few items, when it has many items the Button must be floating and sticky at the end of the screen.

and if the items are few, where there is an empty space in the screen, the button must appear as the final element in the RecyclerView list.

the button is a rectangular raised button with a background. is there any suggestion how to achieve something like this.

Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44

3 Answers3

1

doing like that

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_gravity="bottom|center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

That's the result when has many item

enter image description here

and when has few item

enter image description here

1

You can do something like this,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weight_sum ="10">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"/>
        <Button
            android:text="button"
            android:layout_width="match_parent"
            android:layout_height="0dp" 
            android:layout_weight="1"/>
</LinearLayout>

this is same as answer above. But difference is that the button will be always aligned at the bottom with weight 1.

Rashiq
  • 650
  • 1
  • 9
  • 23
0

One option is to have the button floating the whole time above the list view, and position it manually to make it appear to be at the bottom of the list. if you control the position you can determine when it should stick at the bottom.

see this post on how to get the height of an view at runtime.

view.measure(ViewGroup.LayoutParams.WRAP_CONTENT,
  ViewGroup.LayoutParams.WRAP_CONTENT);

int height=view.getMeasuredHeight();

and see this article for how to get the screen height

int screenHeight = getWindowManager().getDefaultDisplay().getHeight()

then in your layout.xml you can put

`android:layout_alignParentBottom="true"` on the element

then use this information to position the button

and now the magic happens

int offset = screenHeight - ButtonHeight - listHeight;
if(offset < 0) offset = 0;
btn.setTop(offset);
Fire Crow
  • 7,499
  • 4
  • 36
  • 35