ScrollView not scrolling at all
Asked Answered
D

16

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.

Derwent answered 11/2, 2013 at 11:9 Comment(6)
can you please paste the complete xml, maybe is something related with your LinearLayout.Monjan
Try changing the android:layout_height="match_parent" to android:layout_height="0dp" and add android:layout_weight = "1" on the ScrollView.Syman
Just tried and no luck.Derwent
For users of Constraint layout, see this linkAlpinist
As @SiddharthLele suggested, setting the layout_height to 0dp did the trick for meContradistinction
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.Zither
D
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>
Derwent answered 11/2, 2013 at 13:6 Comment(5)
Thanks! However stupid of Android this is, I wouldn't have figured out ever that this can be one of the reasons too -_-Burberry
Also need to update the scrollView's layout_height to wrap_contentHubris
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.Grande
I think this was true in 2013,but now it works on newer Android versionsArdell
If your main view is ConstraintLayout and it has a padding - in my case was bottom padding -, scroll won't work. What I did is, I added another layout and pinned it to the main one top, bottom ,right, left and then added the scrollview as child of the second layout and pinned it again to four sides and set height to 0dp, it worksMinorca
M
24

❌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>
Maupin answered 4/1, 2021 at 1:47 Comment(0)
P
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>
Piggish answered 31/10, 2018 at 8:2 Comment(2)
OMG I've been struggling with this for like an hour now. This is gold.Evocation
Worked for me as well! Thank you!Mandamus
G
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"
Grand answered 24/1, 2018 at 21:44 Comment(1)
How will one know how much padding will be enough? I mean since all devices are different wouldn't it cause problems?Promethium
S
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.

Strappado answered 11/2, 2013 at 11:23 Comment(0)
C
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.

Clyte answered 10/10, 2015 at 18:26 Comment(0)
V
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.

Volcanic answered 9/6, 2017 at 7:9 Comment(0)
B
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" >
Bike answered 27/4, 2019 at 4:30 Comment(0)
M
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

Maffick answered 24/8, 2018 at 13:0 Comment(0)
V
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>
Votive answered 14/7, 2017 at 8:1 Comment(0)
B
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

Bloch answered 26/9, 2017 at 18:42 Comment(0)
S
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>
Strobilaceous answered 28/9, 2020 at 19:30 Comment(2)
Don't add any margin and padding in the layoutsStrobilaceous
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.Brockington
C
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">
Cogon answered 24/8, 2021 at 17:15 Comment(0)
F
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">
Floristic answered 15/9, 2021 at 9:54 Comment(0)
S
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"
Sextodecimo answered 25/4, 2022 at 23:11 Comment(0)
B
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>```
Bibliolatry answered 16/9, 2022 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.