How can I scroll vertically the ConstraintLayout?

The content of the main activity looks like this:

the main screen looks like (it's all inside a ConstraintLayout):

  • image title
  • date
  • the image itself
  • image description

The image description might be too long to display, to fix that, I can put it in a ScrollView and it works fine. However, I would like to scroll through the whole ConstraintLayout and it doesn't work as expected: cannot scroll and some TextViews don't appear!

I am new to Android development; help would be appreciated!


Relevant code:

<android.support.constraint.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:id="@+id/desciptionScroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
    tools:showIn="@layout/activity_nasa_daily_image">

 <TextView
    android:id="@+id/imageTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/test_image_title"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 <TextView
    android:id="@+id/imageDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/test_image_date"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageTitle" />

 <ImageView
    android:id="@+id/imageDisplay"
    android:layout_width="368dp"
    android:layout_height="408dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="3dp"
    android:src="@mipmap/test_image"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageDate" />

<TextView 
    android:id="@+id/imageDescription"
    android:layout_width="368dp"
    android:layout_height="0dp"
    android:layout_marginLeft="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="4dp" 
    android:text="@string/test_image_description" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />

</android.support.constraint.ConstraintLayout>    

      

+7


source to share


4 answers


To make the items on the screen scrollable, I think you could use the NestedScrollView inside the CoordinatorLayout. I usually put a LinearLayout with all the elements that I would display there.

For example:



<android.support.design.widget.CoordinatorLayout
    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"
    tools:context="com.xengar.android.puzzlewildanimals.ui.HelpActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

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

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

        <android.support.constraint.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:id="@+id/desciptionScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.picture.nasa.nasadailyimage.NasaDailyImage"
            tools:showIn="@layout/activity_nasa_daily_image">


            <TextView
                android:id="@+id/imageTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:text="@string/test_image_title"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/imageDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:text="@string/test_image_date"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageTitle" />

            <ImageView
                android:id="@+id/imageDisplay"
                android:layout_width="368dp"
                android:layout_height="408dp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="3dp"
                android:src="@mipmap/test_image"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageDate" />

            <TextView
                android:id="@+id/imageDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:text="@string/test_image_description"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageDisplay" />




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

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

      

If that doesn't work, you can replace ConstraintLayout with LinearLayout. Hope it helps.

+11


source


To add scrolling to any view, you add a ScrollView. In your code add it as root (if ScrolLView is not root and more content, add ScrollView around ConstraintLayout). move the namespaces (lines containing xmlns

) to the new root. Add width and height to ScrollView to match_parent (or whatever you have) and set ConstraintLayout height to wrap_content.

However, you won't be able to scroll properly in design mode. ( link ). But it will still work as expected on the device.



Note:

If your layout is not scrolling with SCrollView make sure the ConstraintLayout height is wrap_content

+1


source


Not necessary to use NestedScrollView

if you can force the constraint layout to "expand" outside of its parent ScrollView as described in this related SO answer

0


source


surrounded my constrinat layout with a ScrollView tag and gave it a property android:isScrollContainer="true"

.

-1


source







All Articles