Search code examples
androidfloating-action-button

Implement floating action icon/button which allows to go (or focus or scroll up) to the top of the page


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.


Solution

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