thomaus
thomaus

Reputation: 6258

ScrollView not scrolling at all

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.

Upvotes: 40

Views: 96487

Answers (16)

Vishnu V R
Vishnu V R

Reputation: 1

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>```

Upvotes: 0

Salah
Salah

Reputation: 149

Add:

android:layout_weight="0"

In:

<ScrollView

.

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

Upvotes: 0

NMK
NMK

Reputation: 1

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">

Upvotes: 0

Aman Thakur
Aman Thakur

Reputation: 59

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">

Upvotes: 0

Sarang M K
Sarang M K

Reputation: 311

❌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>

Upvotes: 25

codeByBunny
codeByBunny

Reputation: 11

    <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>

Upvotes: 0

thomaus
thomaus

Reputation: 6258

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>

Upvotes: 42

Mayur Dabhi
Mayur Dabhi

Reputation: 3926

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" >

Upvotes: 2

Varun Chandran
Varun Chandran

Reputation: 671

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>

Upvotes: 12

Jani
Jani

Reputation: 51

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

Upvotes: 1

Gutimore
Gutimore

Reputation: 385

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"

Upvotes: 3

Carlos Daniel
Carlos Daniel

Reputation: 2699

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

Upvotes: 0

Angoranator777
Angoranator777

Reputation: 374

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>

Upvotes: 0

Ashwin
Ashwin

Reputation: 7647

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.

Upvotes: 2

Ocean
Ocean

Reputation: 21

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.

Upvotes: 2

TNR
TNR

Reputation: 5869

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.

Upvotes: 2

Related Questions