Search code examples
androidxmlandroid-layoutandroid-constraintlayoutandroid-scrollview

ConstraintLayout inside a Scrollview - Problem


I have been trying to figure out for quite some time, when ever i try to move views inside a scrollview, it goes all over the place.

Here are the xml code and the video with the behavior that illustrates it better, basically i can't add more views as it's goes all over the place, and it keeps stretching my recycleviews and messing up the views.

I want to be able to add more than two recyclerviews beyond the fragment's view.

Link to the video

<ScrollView 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:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:background="#1C212F"
        android:clickable="true"
        android:focusable="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        tools:context=".ui.home.HomeMovieFragment">

        <TextView
            android:fontFamily="@font/alfa_slab_one"
            android:id="@+id/tvHomeTitle"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:layout_width="wrap_content"
            android:text="@string/explore"
            android:textColor="@color/white"
            android:textSize="22sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:fontFamily="@font/cutive"
            android:id="@+id/tvHomeName"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_width="wrap_content"
            android:text="@string/welcome_back"
            android:textColor="@color/white"
            android:textSize="12sp"
            app:layout_constraintStart_toStartOf="@+id/tvHomeTitle"
            app:layout_constraintTop_toBottomOf="@+id/tvHomeTitle" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.13" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.62" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMoviesHome"
            android:layout_height="w"
            android:layout_width="0dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline2" />

        <TextView
            android:fontFamily="@font/alfa_slab_one"
            android:id="@+id/textView4"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_width="wrap_content"
            android:text="@string/top_rated"
            android:textColor="@color/white"
            android:textSize="14sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline6"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline3" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_height="40dp"
            android:layout_marginEnd="16dp"
            android:layout_width="40dp"
            android:src="@drawable/temp_image"
            app:layout_constraintBottom_toBottomOf="@+id/btnLogout"
            app:layout_constraintEnd_toStartOf="@+id/btnLogout"
            app:layout_constraintTop_toTopOf="@+id/btnLogout" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline6"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.69" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.93" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvTopRated"
            android:layout_height="0dp"
            android:layout_width="0dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline7"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline6" />

        <ImageButton
            android:background="@null"
            android:foreground="?android:attr/selectableItemBackground"
            android:id="@+id/btnLogout"
            android:layout_height="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="16dp"
            android:layout_width="40dp"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/icon_logout" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

Thank you in advance.


Solution

  • Finally found a solution, the guidelines were to blame as soon i removed them, everything started to work as expected, and i also changed the RecycleViews's height to wrap_content.