5

Somehow I'm not able to fully scroll inside my NestedScrollView. Inside my NestedScrollView is a TextView with a lot of text and a button below it. When I scroll down I can only see a the top part of the button (only a few pixels). It doesn't scroll far enough.

This is my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:stateListAnimator="@drawable/appbar_always_elevated"
        android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <TextView
                android:id="@+id/textView"
                style="@style/Text.Body1.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:text="@string/large_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/appbar"/>

        <Button
                android:id="@+id/continueButton"
                style="@style/Widget.Button.Green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/continue_text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
    </LinearLayout>
   </android.support.v4.widget.NestedScrollView>

  </android.support.design.widget.CoordinatorLayout>
Stephen
  • 9,899
  • 16
  • 90
  • 137
Peter Fortuin
  • 5,041
  • 8
  • 41
  • 69
  • https://stackoverflow.com/questions/34306559/nestedscrollview-wontt-scroll-to-the-end-when-used-with-collapsingtoolbarlayout – Adeel Ahmad Dec 03 '18 at 17:30
  • I have removed `android:stateListAnimator="@drawable/appbar_always_elevated"` in `AppBar` and constraint attributes in `TextView` and `Button`. No problem founded. It works fine. I think you probably miss some info in question which causes the problem. – aminography Dec 07 '18 at 07:38
  • @Peter, can you post a simple project at github which reproduces the issue? – azizbekian Dec 07 '18 at 11:09
  • Has this question been answered for you or are you still loooking for a solution? – Cheticamp Dec 11 '18 at 17:50
  • use Relative layout and chack that your layout use full height – Lokesh Dec 12 '18 at 06:48

6 Answers6

0

Change the height of the LinearLayout inside your NestedScrollView to be wrap_content.

The idea behind NestedScrollView and its single scrolling child is that the outside (the scroll view) should have some fixed / predetermined height, and the inside (the scrolling child) should have a dynamic height that is larger than the parent. After all, if the content isn't larger than the parent, then what's scrolling?

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • While your reasoning sounds correct, it doesn't solve my problem. What I see in the layout inspector is that the NestedScrollView is too big. It has the same height as the CoordinatorLayout and that is not correct of course, because the AppBarLayout (with the toolbar) is also in there. – Peter Fortuin Dec 04 '18 at 11:10
  • 1
    So I guess the height at the bottom that I'm missing is the same height as the collapsed toolbar. – Peter Fortuin Dec 04 '18 at 11:23
0

You need to add android:fitsSystemWindows="true" in your CoordinatorLayout , AppBarLayout and CollapsingToolbarLayout ,

Read more about fitsSystemWindows

Why would I want to fitsSystemWindows?

Try this

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="@string/app_name">

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/large_text" />

            <Button
                android:id="@+id/continueButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="continue_text" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

This is my working .xml code .

Please Apply it.

strings.xml

<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>

main_activity.xml file

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <!--app:contentScrim="?attr/colorPrimary"-->


                <RelativeLayout
                    android:id="@+id/lay"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/inhouse_ad_card_height"
                    android:background="@drawable/app_gradient_square"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:visibility="visible"
                    app:layout_collapseMode="parallax"
                    app:layout_scrollFlags="scroll|enterAlways">

                <!--write tag here-->

                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@drawable/bg_transition"
                    android:visibility="visible"
                    app:contentInsetEnd="@dimen/dim_10"
                    app:contentInsetStart="@dimen/dim_10"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    <RelativeLayout
                        android:id="@+id/layBtns"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">

                        <ImageView
                            android:id="@+id/btnSetting"
                            android:layout_width="@dimen/btn_height"
                            android:layout_height="@dimen/btn_height"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:gravity="center"
                            ads:srcCompat="@drawable/ic_editor_settings" />

                        <TextView
                            android:id="@+id/txtAppTitle"
                            style="@style/TitleFont"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/img_btn_height"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:text="@string/display_name"
                            android:textColor="@color/white"
                            android:textSize="@dimen/font_size_extra_large"
                            android:visibility="gone" />

                    </RelativeLayout>

                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

                    <!--write tag here-->

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </android.support.design.widget.CoordinatorLayout>
Prince Dholakiya
  • 3,255
  • 27
  • 43
0

Just change your NestedScrollView to this:

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
0

Try this

   private void updateListViewHeight(ListView myListView) {

    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null) {
        return;
    }
    //get ListView height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < adapterCount; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);

}

Pass your listview in the above method.

rak
  • 108
  • 1
  • 12
0

For everyone's reference, since I just had the same problem. This is a known issue and has been fixed in com.google.android.material:material:1.3.0-alpha04

https://github.com/material-components/material-components-android/commit/a21a30026a33fc20cf7ad699d32d1298b84096c6

Alessandro Mulloni
  • 1,011
  • 1
  • 9
  • 10