sneps
sneps

Reputation: 427

ScrollView not scrolling with ConstraintLayout and guideline

I have to make a layout with an image on top and text above. The images height has to be 40% of the root layout height. The text has variable height. Therefore the whole thing has to be put in a ScrollView.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTutorialGrey"
>


<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true"
    >

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

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

        <ImageView
            android:id="@+id/tutorial_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/guideline"
            tools:src="@drawable/splash_bg"
            />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="32dp"
            app:layout_constraintTop_toBottomOf="@id/guideline"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            >

            <TextView
                android:id="@+id/tutorial_title"
                style="@style/Tutorial.Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:text="Alle Neuerungen der App auf einen Blick"
                />

            <TextView
                android:id="@+id/tutorial_description"
                style="@style/Tutorial.Description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet."
                />

        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</ScrollView>
</android.support.constraint.ConstraintLayout>

The layout is like it is desired, but as soon as i put in the Guideline, the ScrollView does not scroll anymore.

What mistake do i make and how can i achieve the desired layout and behaviour?

Upvotes: 2

Views: 1141

Answers (1)

Cheticamp
Cheticamp

Reputation: 62831

The guideline and the image view must have the same ConstraintLayout as a parent. Move the guideline just above the image view and you should be set.

On a side note, you probably don't need the outermost ConstraintLayout. You can also probably get rid of the nested LinearLayout for a flatter layout.


The guideline you have set is based upon a percentage. Unfortunately, this percentage seems to be a fraction of the total scrollable height of the view and not just what you see on the screen.

You want the image to take up 40% of the scroll view's visible portion. I suggest that you set the guideline in the XML to be a fixed size (app:layout_constraintGuide_begin="100dp" for instance) and change the guideline placement in code doing something like the following:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrollable);
    final ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);

    layout.post(new Runnable() {
        @Override
        public void run() {
            final ScrollView scrollView = (ScrollView) layout.findViewById(R.id.scrollView);
            final Guideline guideline = (Guideline) findViewById(R.id.guideline);
            final ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
            lp.guideBegin = (int) ((scrollView.getHeight()) * 0.4);
            guideline.setLayoutParams(lp);
        }
    });
}

I would suggest using ConstraintSet#setGuidelineBegin, but I can't get that to work. You may have better luck.

Upvotes: 2

Related Questions