-1

I want to put a scroll view to scroll the content on my screen, but I can't understand what I'm doing wrong here. Any help would be highly appreciated. Do I need to put my constraint layout in a linear layout and then that linear view in the scroll view or I'm doing something else wrong here?

 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/layout_fragment_head"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/color_theme_main_dark"
                app:layout_constraintBottom_toTopOf="@+id/guideline15"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                // I edited the code here for simplicity

            </android.support.constraint.ConstraintLayout>

            <fragment
                android:id="@+id/fragment_deviceslist"
                android:name="com.resatech.android.scoutandroid.master.fragments.DevicesListFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideline15"
                tools:layout="@layout/fragment_devices_list" />

            <android.support.constraint.Guideline
                android:id="@+id/guideline15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent=".3" />


            </android.support.constraint.ConstraintLayout>

         </ScrollView>
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
  • 1
    You might want to have a look at this, since constraint layouts inside scroll views seem to present some difficulties: https://stackoverflow.com/questions/37349845/is-it-possible-to-put-a-constraintlayout-inside-a-scrollview – vlz Apr 16 '19 at 05:24
  • I did but still no help :/ – Muhammad Abdullah Apr 16 '19 at 05:43
  • Possible duplicate of [Android - how to make a scrollable constraintlayout?](https://stackoverflow.com/questions/43098150/android-how-to-make-a-scrollable-constraintlayout) – Karan Harsh Wardhan Apr 16 '19 at 05:49
  • scrollView should contain only one child View, otherwise use Nested ScrollView – Navin Kumar Apr 16 '19 at 06:01

2 Answers2

1

You should not put a ConstraintLayout inside a ScrollView. Otherwise, objects inside ConstraintLayout cannot scroll because they are constrained.

Put the ScrollView inside the ConstraintLayout, then, all objects inside ScrollView can be scrolled (because they are not constrained). In this case, the only constrained view is the ScrollView and this is correct.

If you want a fixed header, put it outside the ScrollView constrained to top of the ConstraintLayout.

Ferran
  • 1,442
  • 1
  • 6
  • 10
  • if i do that.. It just gives the error the Scrollview can only contain one direct child. – Muhammad Abdullah Apr 16 '19 at 05:43
  • Yes, the `ScrollView` should contain only the fragment and the header outside the `ScrollView`. Of course, the `GuideLine` outside the ScrollView too. – Ferran Apr 16 '19 at 06:08
0

in ScrollView just add one layer!
To fix your problem edit your code like this :

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

        <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">

                /* Your Code of Constraint Layout Like :
                <RelativeLayout />
                <ImageView />
                <LinerLayout/>
                and ....  */

        </RelativeLayout>

</ScrollView>
hmotamed
  • 11
  • 5