0

Can anyone suggest me how to implement floating action icon/button which allows to go (or focus or scroll up) to the top of the page when clicked?

Thanks in advance for any help.

Tayyab Amin
  • 686
  • 10
  • 28
  • check this out https://stackoverflow.com/questions/4119441/how-to-scroll-to-top-of-long-scrollview-layout – Shai Sep 12 '18 at 08:59
  • @tApja thanks for your help – Preetam Khangembam Sep 12 '18 at 09:00
  • Welcome to stack overflow. To enhance your question and to prevent to get voted down, please add some more details to your question. In example what have you done so far? Show us the code that is not working for you etc.. If you just started to code for native android, please have a look at google or the search bar on how to implement a FAB and how to scroll up in a scroll and recyclerview. Cheers – JacksOnF1re Sep 12 '18 at 09:12
  • Thanks a lot for your suggestion @JacksOnF1re. I need to improve my googling skills. – Preetam Khangembam Sep 12 '18 at 09:22

1 Answers1

3

Add this code to your layout file:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:src="@drawable/ic_my_icon"
    android:layout_margin="16dp" />

You can then apply an View.OnClickListener to handle FAB taps.

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View view) 
    {
        scrollView.fullScroll(ScrollView.FOCUS_UP); //code to scroll to the top of scrollview
    } 
});
Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34