Search code examples
androidandroid-constraintlayoutandroid-tablayout

Why does a layout_constraintStart_toEndOf keep overlapping the first control in a ConstraintLayout?


I'm designing a view where I have the picture on the top taking 33% of the screen and the rest of the details 66%. However, the details section keeps overlapping the picture like this:

preview

This is my code:

<androidx.constraintlayout.widget.ConstraintLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/llImg"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent="1">
        <ImageView
            android:id="@+id/imgBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />
        
        <TextView
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:textStyle="italic"
            android:textSize="12dp"
            android:layout_alignParentBottom="true"
            android:textColor="#4c8c4a"
            android:id="@+id/lblImgDescription" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/llTab"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".66"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/llImg">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mTabOptions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

I did what thought should be the logical action to add an ID (llImg) in the first FrameLayout and in the bottom LinearLayout I indicated the one where it should start at its bottom: app:layout_constraintStart_toEndOf="@+id/llImg". Any idea what I'm doing wrong?


Solution

  • app:layout_constraintTop_toBottomOf="@+id/llImg" that's what u r missing overall . i made few changes try the layout below . Also u r missing 1% of the screen that will occupy as margin top/bottom for llTab so keep this in mind.

    <androidx.constraintlayout.widget.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"
    >
    
    <FrameLayout
        android:id="@+id/llImg"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <ImageView
            android:id="@+id/imgBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />
    
        <TextView
            android:id="@+id/lblImgDescription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:textColor="#4c8c4a"
            android:textSize="12dp"
            android:textStyle="italic" />
    </FrameLayout>
    
    <LinearLayout
        android:id="@+id/llTab"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".66"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/llImg">
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mTabOptions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>