(Android) ScrollView won't scroll all the way to the bottom of my LinearLayout
Asked Answered
A

5

9

So I have a ScrollView with a LinearLayout within it. It seems that when I attempt to scroll to the bottom of my linearlayout, the bottom ~5 dip is cut off (ie bottom margin) I think it might have something to do with my linearlayout's 5dip margin?

Here's activity_create_account.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@drawable/grey"
android:orientation="vertical"
android:padding="0dp"
tools:context=".Login" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">

<!-- BEGIN HEADER -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/titlebar"
    android:orientation="horizontal"
    android:padding="8dip" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="0dp"
        android:text="create account"
        android:textColor="#FFFFFF"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
<!-- END HEADER -->

<!-- BEGIN BODY -->

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/innerLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:background="@drawable/rounded_white"
    android:orientation="vertical"
    android:padding="5dip" >

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
        android:id="@+id/etChooseUsername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a username"
        android:inputType="text"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etChoosePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etRetypePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Re-type password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />

    <Button
        android:id="@+id/bCreateAccountConfirm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:padding="0dip"
        android:text="Create Account"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
</ScrollView>
<!-- END BODY -->

Here's what it looks like when I try to scroll all the way down (there's supposed to be a slim section of white beneath the button & then a slim section of grey margin) enter image description here

Amy answered 27/6, 2013 at 7:53 Comment(4)
instead of having android:layout_margin="10dip" on the android:id="@+id/innerLinearLayout", put android:padding="10dip" to the ScrollViewEurhythmy
@Eurhythmy when I put padding on my ScrollView, it still cuts off the bottom of my inner layout (and leaves an unsightly 10dip-wide grey section at the bottom of the screen no matter where it's scrolled)Episcopacy
I might have read the question a bit wrong, forget the change I said about the 10dp padding for the scrollview, sry. You want for the "create account" button to have a small margin at the bottom, so put a android:layout_marginBottom="10dp" on the android:id="@+id/bCreateAccountConfirm"Eurhythmy
In my app, i set margintop in LinearLayout and there is the same problem. Then I set paddingTop, it works. I thinks it's a bug because margintop make the layout totally move down!Tc
E
12

In your ScrollView add padding_bottom to some 10dp. It would work.

Else the view remaining below the HorizontalView might be overlaying above this horizontal view. In that case

  1. Add a id to HorizontalView id="@+id/horizontalView"
  2. Add below="@+id/horizontalView" in the view below horizontal view.
Easygoing answered 27/6, 2013 at 8:44 Comment(3)
What do you mean by the "HorizontalView"?Tewell
It might have been changed to "HorizontalScrollView" I supposeEasygoing
Why is adding padding necessary? I understand why it works, but why is the scrollview broken in the first place? Edit: I solved my instance of this problem by constraining the bottom of the scrollview to the bottom of the parent. So I guess the view was larger than the parentJerlenejermain
S
18

I realize that this question is a little old - but for the sake of those who will find this through Google I will try to shed some light.

It seems as though the generic ScrollView object does not play well when used inside a layout with other siblings. Thus, Google has created a NestedScrollView. From the documentation,

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.

I had a similar problem to the one this question poses and using NestedScrollView eliminated the cut-off at the bottom of my original ScrollView.

Schuh answered 23/8, 2016 at 1:23 Comment(2)
Thanks! Saved me a lot of time and worked like a charmFallon
Sadly, this is crashing my app. Not sure why. Any thoughts?Sapodilla
E
12

In your ScrollView add padding_bottom to some 10dp. It would work.

Else the view remaining below the HorizontalView might be overlaying above this horizontal view. In that case

  1. Add a id to HorizontalView id="@+id/horizontalView"
  2. Add below="@+id/horizontalView" in the view below horizontal view.
Easygoing answered 27/6, 2013 at 8:44 Comment(3)
What do you mean by the "HorizontalView"?Tewell
It might have been changed to "HorizontalScrollView" I supposeEasygoing
Why is adding padding necessary? I understand why it works, but why is the scrollview broken in the first place? Edit: I solved my instance of this problem by constraining the bottom of the scrollview to the bottom of the parent. So I guess the view was larger than the parentJerlenejermain
V
4

You can set marginBottom to your scrollview

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">
Vietnam answered 27/6, 2013 at 8:5 Comment(3)
I had a similar issue, and it was due to the margin on the textview inside the scroll view. Removing the margin from the textview, and adding it to the scrollview meant all text was visible and scroll worked as expected. In my case, I had 20dp margin so an entire line was missing, so I removed it from bottom margin of textview, and added to scroll.Analyzer
In my case the reason was that TableLayout was using dividers with height of 2dp. I had 30 rows, so I had to add marginBottom of 60dp and it scrolled exactly till the end including the last rowHazy
It's a bit of hack, but it works and it's the simplest solution compared to figuring out the "voodoo behaviour" of the Android render engine.Hoptoad
S
3
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"

really solved the issue for me, no NestedView or padding is required. Keep ScrollView's direct child view's layout_height as "wrapped_content" though.

Serow answered 24/4, 2021 at 9:17 Comment(0)
M
0

A short delay solved my problems with this, it seems that telling the view to scroll to the bottom immediately after adding an item can miss the new item and scroll one short

        mView.postDelayed(new Runnable() {
            @Override
            public void run() {
                sv.fullScroll(View.FOCUS_DOWN);
            }
        },200);
Macneil answered 17/3, 2023 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.