Search code examples
androidandroid-layoutscrollview

Align child in ScrollView to Bottom or scroll


I was having the same problem discussed over here Align Button to Bottom in ScrollView but with native Android.

Imagine you have following layout:

Content smaller than height

As the content might not fit on smaller screens/configurations you wrap your content in a scrollview. Plus: To avoid that users miss critical content which is below the fold you want that the CTA is positioned below the content.

Screen Layout
enter image description here ScrollView:
- ConstraintLayout
-- //content
-- Button

But now the CTA is not aligned to the bottom of the screen anymore. For taller devices where no scrolling is necessary it would look like this:

enter image description here

How can we get the button still be positioned on the bottom when the content is not scrolling?


Solution

  • It is pretty simple to achieve that:

    Make your ScrollView grow the whole screen by using android:fillViewport="true" This will stretch the child (ConstraintLayout) to fill the whole screen if it is smaller.

    enter image description here

    Now that the child of the scrollview is full-height, you can align the CTA to the bottom of it's parent

    Screen Layout
    enter image description here ScrollView:
    - ConstraintLayout
    -- //content
    -- Button(toBottomOf=parent)

    And if the content scrolls, the button will still align below it.