ScrollView not Scrolling - Android
Asked Answered
B

9

14

I am unable to scroll my scrollview. It has a textView, an imageview and few linear layouts inside of it. When I replace the imageview and linear layouts with textview it is working. Here is my code:

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:scrollbars="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Drop Text Down"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="555dp"
            android:src="@drawable/ic_launcher" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ln"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#000000"
            android:orientation="vertical" >

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

                <TextView
                    android:id="@+id/TextView06"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

</LinearLayout>
Bacillus answered 17/5, 2015 at 1:57 Comment(6)
Try changing the layout_height of your ScrollView to wrap_contentBernal
@Bernal A i have to use layout weight inside the scrollview. so i cant set the height to wrap_contentBacillus
@Bacillus did you find a solution to this?Scenography
I find it also frustrating and not only that.. Its mind blowing how can a scrollView not scroll :D How can a computer not compute ? There is only one answer to that :) And YES, I have set that magical layout_height to magical value wrap_content. Yet, ain't scroll.Enervate
I have figured, that when I scroll a mouse wheel on the not performing ScrollView (in android emulator), it DOES SCROLL. And Lo and Behold, after this, it does respond on finger touch-scrolling too. LOL.. Why ? Cuz its one big - buggy mess.Enervate
@Bacillus please accept any of the answer that worked for you or post your answer which worked, that will help others readersPrier
P
24

Put an empty view with fixed height

<View
                android:layout_width="match_parent"
                android:layout_height="50dp" />

as your last item in the linear layout which is a child of scroll view..

This is just a trick, I don't know the logic. Maybe, in the presence of bottom navigation bar, the scrollview behaves like that. So if we add another view with height that matches the bottom navigation bar's height(40-50) it performs well.

Prier answered 22/9, 2017 at 8:6 Comment(0)
B
12

The child view of a ScrollView should be set to wrap_content. If you set it to match_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.

Try changing the child LinearLayout layout_height to either wrap_content or a specific size (in dp) instead of match_parent.

Bernal answered 17/5, 2015 at 3:4 Comment(1)
Nop does not help allways.. This is just one of apparently several problems with this element of ADT...Enervate
M
3

Your ScrollView child needs to have its height as wrap_content :

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

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

    ... 
    ...
</LinearLayout>
</ScrollView>
Moonscape answered 17/5, 2015 at 5:59 Comment(0)
H
3

I was able to solve it by adding (android:windowSoftInputMode="adjustResize|stateHidden") in the activity manifest, like below

<activity
            android:name=".ui.main.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
        </activity> 
Huai answered 30/10, 2018 at 11:27 Comment(0)
B
3

Maybe i'm too late for this, but the only method that helped me fix the scrollview not scrolling after hours of searching and trial and error method is, setting scrollview's height to 0dp.

Here's an example xml code for reference.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
  • The top constraint layout having "match_parent" will fit the screen.
  • The Scroll View inside having "0dp" adjusts the height to the content inside and creates a scrollable view when the content goes beyond the top/ parent constraint layout.
  • The inner most constraint layout (child of scroll view) will contain the views and actual contents.
Bassesalpes answered 5/4, 2021 at 14:50 Comment(1)
Perfect solution for when your ScrollView is inside a ConstraintLayoutNickelodeon
A
2

You should set the height of LinearLayout (child of Scrollview) to wrap_content.

When the child is taller than the ScrollView, then android:fillViewport="true" attribute has no effect.

Artair answered 17/5, 2015 at 6:23 Comment(2)
I know that. But I want to set Layout weight for these layouts so i Have to use fillViewPort.Bacillus
@Bacillus may be you can try android:layout_height="wrap_content" for the imageview with id="imageView1"Artair
I
0

If you set the height of the Linear layout inside the scrollview to match_parent, there is nothing to scroll if the height is the same as the scrollview.

You have to set it to wrap-content. This way the height of the LinearLayout will be greater than the scrollview, and you will then be able to scroll.

Corect Code

<ScrollView
    android:id="@+id/scrollView1"
    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:animateLayoutChanges="true"
        android:orientation="vertical"
        android:scrollbars="vertical" >

Aditional Note: You also cannot use weight and weightsum inside a scrollview. Doing so will make the scrollview unscrollable.

Ibnrushd answered 30/9, 2021 at 15:4 Comment(0)
N
-3

use Table Layout instead of linear layout something like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:fadeScrollbars="false" android:padding="6dip">
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    >   
   //your stuff 
</TableLayout>
</ScrollView>
Necrolatry answered 17/5, 2015 at 4:53 Comment(1)
there is nothing to do with linear layout or table layoutVeriee
I
-3
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

Remove android:fillViewport="true" from above element

Inscrutable answered 7/6, 2016 at 8:32 Comment(1)
Nop, I have many scrollViews and some of them work, som do not, invariantly on this parameter.Enervate

© 2022 - 2024 — McMap. All rights reserved.