2

I have a scrollview inside my page and that contains a linearlayout. My requirement is to add a floating button on scrolling this layout and perform actions on this button click. How can this be done. Please help me out. Thanks in advance.

Anju
  • 9,379
  • 14
  • 55
  • 94

2 Answers2

4

Perhaps I've misunderstand your question, but it sounds like you want a stationary Button outside of your ScrollView so that you can scroll and always see the Button?

If so, just add the Button OUTSIDE the ScrollView:

<Button
    android:id="@+id/button_save"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Save" />

<ScrollView
    android:id="@+id/scrollView_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </LinearLayout>
</ScrollView>
Fifer Sheep
  • 2,900
  • 2
  • 30
  • 47
  • Outside the button and scrollview, a relativelayout can be used to wrap. And the Button can be used with android:layout_alignParentBottom="true",android:layout_alignParentRight="true" to align to the rightbottom position of parent. – juejiang Sep 29 '15 at 02:28
3

Use this library, it has already done things you required.

Include it in your project:

dependencies {
    compile 'com.shamanland:fab:0.0.5'
}

Add FloatingActionButton to your layout instead of regular Button:

<com.shamanland.fab.FloatingActionButton
    android:id="@+id/button_save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

Link it with ScrollView:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.button_save);
ScrollView scrollView = (ListView) findViewById(R.id.scrollView_list);
scrollView.setOnTouchListener(new ShowHideOnScroll(fab));

That's all, it should works!

Check my original post here and look at the example app.

Community
  • 1
  • 1
Oleksii K.
  • 5,359
  • 6
  • 44
  • 72