Snusifer
Snusifer

Reputation: 553

XML constraint layout: Elements placed on top of each other, fix?

My XML code looks like this:

<?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:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".BottomNavActivity">

    <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:background="#CCB0F0"
            android:text="Map"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/idCardView" app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.v7.widget.CardView
            android:id="@+id/idCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="5dp"
            app:cardCornerRadius="4dp" app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="5dp" android:layout_marginBottom="332dp"
            app:layout_constraintTop_toBottomOf="@+id/message"
            app:layout_constraintBottom_toTopOf="@+id/container">
        <fragment android:id="@+id/autocomplete_fragment"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        />
    </android.support.v7.widget.CardView>

    <android.support.constraint.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/container"
            app:layout_constraintTop_toBottomOf="@+id/idCardView"
            app:layout_constraintBottom_toTopOf="@+id/navigation" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0">

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation_menu"/>



</android.support.constraint.ConstraintLayout>

I want the TextView on top, followed by the CardView, followed by a ConstraintLayout, followed by the bottomNavigationView. Somehow the elements are stacked on top of each other like this:

PhoneEmulator

I made sure that every element are constrained to the top or bottom of the next element respectively. Still they end up on top of each other. Is there a better way or a fix to this problem?

Upvotes: 1

Views: 1768

Answers (2)

Ferran
Ferran

Reputation: 1448

  • You don't need to constraint TextView to Carview, only CardView to TextView.
  • If TextView width is match_parent, you don't need to set right constraint. Only top and left.
  • remove CarView to parent top constraint
  • you don't need to constraint CardView to Container, only container to CardView

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="#CCB0F0"
        android:text="Map"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
    />
    
    <android.support.v7.widget.CardView
        android:id="@+id/idCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="5dp"
        app:cardCornerRadius="4dp" 
        tools:layout_editor_absoluteX="5dp" 
        android:layout_marginBottom="332dp"
        app:layout_constraintTop_toBottomOf="@+id/message"
        app:layout_constraintLeft_toLeftOf="parent"
        >
    
        <fragment android:id="@+id/autocomplete_fragment"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
    />
    

Upvotes: 2

FedeFonto
FedeFonto

Reputation: 354

In card view you added two constraint on top: remove app:layout_constraintTop_toTopOf="parent" and will work.

Upvotes: 2

Related Questions