39

I can't make a ScrollView properly scrolling. It always cut off the content on the bottom, as if it were a normal LinearLayout.

My code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:orientation="vertical" >

Of course, I already tried to add / remove the "fillViewport" and "isScrollContainer" properties, and it did not change anything at all.

This is for ScrollView (VERTICAL ONLY)

HorizontalScrollView must be used for horizontal scrolling.

Fattie
  • 27,874
  • 70
  • 431
  • 719
thomaus
  • 6,170
  • 8
  • 45
  • 63
  • can you please paste the complete xml, maybe is something related with your LinearLayout. – PaNaVTEC Feb 11 '13 at 11:14
  • 1
    Try changing the `android:layout_height="match_parent"` to `android:layout_height="0dp"` and add `android:layout_weight = "1"` on the `ScrollView`. – Siddharth Lele Feb 11 '13 at 11:14
  • Just tried and no luck. – thomaus Feb 11 '13 at 12:56
  • For users of Constraint layout, see this [link](https://stackoverflow.com/questions/42453367/scrollview-inside-constraint-layout-does-not-scroll-to-the-bottom-of-the-parent) – Reejesh PK Jan 10 '19 at 04:32
  • As @SiddharthLele suggested, setting the layout_height to 0dp did the trick for me – ValYouW Jan 28 '20 at 21:30
  • I converted my ConstraintLayout to LinearLayout, adding margins, and I think that's what caused the problem. I just dragged a new scrollview and moved everything to it, fixing my problem. – Mr. Disability Jun 24 '20 at 07:28

16 Answers16

42

Answer: the ScrollView is not working when used as the root element of an XML layout. It has to be wrapped inside a LinearLayout.

Solution then :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>
</LinearLayout>
Marlon
  • 1,839
  • 2
  • 19
  • 42
thomaus
  • 6,170
  • 8
  • 45
  • 63
  • 5
    Thanks! However stupid of Android this is, I wouldn't have figured out ever that this can be one of the reasons too -_- – Aman Alam Dec 02 '16 at 06:45
  • 1
    Also need to update the scrollView's `layout_height` to `wrap_content` – Maxwell Aug 14 '17 at 22:44
  • 5
    This is not true that you cant use `ScrollView` as your root element, you can use `ScrollView` as your Root element of `XML` there is no problem to use it as root element. – Haritsinh Gohil Oct 07 '19 at 10:13
  • I think this was true in 2013,but now it works on newer Android versions – Squareoot Feb 12 '23 at 21:10
21

❌Tried all the above mentioned answers, but still my issue was unsolved. And after some debugging and changes, my issue got fixed that is the ScrollView is getting scrolled.

  • We need not to add any parent element to the ScrollView to make it scroll (as mentioned in some of the other answers here).

✔️The Fix was for my issue✔️

  • changed the height of the ScrollView to 0dp

    android:layout_height="0dp"

  • Constrained the Top and Bottom to parent/respective views/elements

    app:layout_constraintTop_toBottomOf="@+id/imageView4" app:layout_constraintBottom_toBottomOf="parent"

The final xml looks like this

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:layout_constraintBottom_toBottomOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

           <!-- Loong content -->

    </LinearLayout>
</ScrollView>
Sarang M K
  • 261
  • 3
  • 9
11

Scroll View as Parent has no issues. I faced such issue when we add padding/margin to the direct child of scrollview. Keep the child of scrollview with just height and width properties, den it will work fine.

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:orientation="vertical">
</LinearLayout>
</ScrollView>
Varun Chandran
  • 647
  • 9
  • 23
3

The selected answer IS NOT CORRECT!

You CAN use ScrollView as the root view, it doesnt work for you because you are missing the padding.

Add something like:

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
Gutimore
  • 365
  • 3
  • 10
2

remove the android:isScrollContainer in LinearLayout. As per the documentation android:isScrollContainer is used to set the view scrollable. I hope it helps you. Refer this link for definition.

TNR
  • 5,839
  • 3
  • 33
  • 62
2

Android Studio adds a NestedScrollView to the activity file of some of its templates (e.g. Master-Detail). Having a ScrollView in the fragment file and another one in that fragment’s activity file prevents the scroll view from working. Removing the ScrollView in my fragment file and leaving the one in the activity file solved the issue for me.

Ocean
  • 21
  • 3
2

Try this solution, just delay your scroll using post delayed method.

fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:fillViewport="true"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

TestFragment.java

...
private ScrollView mScrollView;
...
mScrollView = (ScrollView) mView.findViewById(R.id.scrollView);
mScrollView.setSmoothScrollingEnabled(true);
...
new Handler().postDelayed(new Runnable() {
    @Override
     public void run() {
         if(isAdded()) {
             // Scroll 1000px down
             mScrollView.smoothScrollTo(0, 1000);
         }
     }
}, 150);

This worked for me, hope this helps.

Ashwin
  • 7,277
  • 1
  • 48
  • 70
2

Try this,

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fillViewport="true" >

<LinearLayout android:id="@+id/scroll_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
1

I finally figured it out took me literally 5 hours building everything step by step and testing it every step at a time ugh....

Anyway if you have a problem with this i found out that setOntouch listener is messing up your code - at least in my case it was...

i have to figure a way around it, but try to delete your ontouch listener and test your code - it has to work

Jani
  • 51
  • 8
0

This fixed my problem: I added android:isScrollContainer="true" to a LinearLayout and Removed the ScrollView Code Before:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    android:id="@+id/scrollViewRecords"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/sheepList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>
</ScrollView>
</LinearLayout>

Code After

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true">

    <LinearLayout
        android:id="@+id/sheepList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>
</LinearLayout>
Angoranator777
  • 334
  • 2
  • 6
0

Is there a way to close this question? it is old and currently the answer marked as valid doesn't have sense. The only valid thing regarding ScrollView right now is this:

A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout. Never add a RecyclerView or ListView to a scroll view. Doing so results in poor user interface performance and a poor user experience.

Taken from official documentation: https://developer.android.com/reference/android/widget/ScrollView.html

Carlos Daniel
  • 2,459
  • 25
  • 30
0
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
......
</LinearLayout>
</ScrollView>
</LinearLayout>
  • Don't add any margin and padding in the layouts – codeByBunny Sep 28 '20 at 19:31
  • The accepted answer already answers the question. Also the closing xml tag should be added - this could be mistaken as 2 layouts inside the parent layout. Also add some description to it. – Haider Malik Sep 28 '20 at 22:35
0

Looks like you're not using some of the properties and rules.

android:layout_height="0dp"
android:scrollbars="none"
android:fillViewport="true"

Also, after this try to wrap all your scrolling content in Constraint Layout.

<tech.loung.views.SlidingScrollView
android:id="@+id/scrollview_settings"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="none"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_layout">
0

In my scenario it worked when I update Linear layout inside the scroll view with Padding=10dp. That's it scroll view Scrolled successfully

<ScrollView
    android:id="@+id/mainScrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/top_layout"
        android:orientation="vertical"
        android:layout_margin="10dp"
        android:padding="10dp">
NMK
  • 1
  • 3
0

Add:

android:layout_weight="0"

In:

<ScrollView

.

  • And set it to "0" as long as you don't want to apply a "layout_weight"
Salah
  • 139
  • 1
  • 4
0

Wasted an hour with this issue my findings are listed below ,hope will help someone.

  1. You can have ScrollView as root tag
  2. You can have ScrollView child of any other layouts.
  3. You can only have single child ScrollView (I prefer LinearLayout)
  4. I prefer keeping height of scroll view to 0dp
  5. When your root is ConstraintLayout set layout_constraintBottom_toBottomOf="parent"
  6. The child view of ScrollView should not have margin or padding.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!-- you Custom toolbar or other non scrollable items here -->

   <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/bpSyncDivider"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@color/colorWhite">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <!-- Items to be scrolled here -->


        </LinearLayout>

    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>```