-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.

2
  • That's a strange design. What if instead you do a floating button same way as done in GMail and many other apps? That would be in accordance to Google guidelines. Commented Jan 10, 2018 at 19:36
  • well if I have the choice, I wouldn't be here :D .. this is the Business requirement Commented Jan 10, 2018 at 19:39

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.