android / codelab-constraint-layout

Constraint Layout Codelab
https://codelabs.developers.google.com/codelabs/constraint-layout/
Apache License 2.0
469 stars 243 forks source link

View width become zero when chain is used in ContraintLayout #55

Open godwinjoseph-wisilica opened 6 years ago

godwinjoseph-wisilica commented 6 years ago

I have two TextViews that are horizontally chained and displayed in a DialogFragment. That two TextView's visiblity set by dynamically, ie, sometimes tvA will be visible and sometimes tvB or both will display.

Here is my xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background_color"
android:minWidth="250dp">

<TextView
    android:id="@+id/tvElement"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/shape_back"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/elements"
    android:textColor="@color/item_view_color"
    app:layout_constraintEnd_toStartOf="@+id/tvSensor"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvSensor"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/shape_back"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/sensors"
    android:textColor="@color/item_view_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tvElement"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@+id/rv_channel"
    app:layout_constraintGuide_begin="62dp"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_channel"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toTopOf="@+id/btn_ok"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_min="250dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/guideline" />

<android.support.v7.widget.AppCompatButton
    android:id="@+id/btn_ok"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/global_button_selector_3"
    android:text="@string/ok"
    android:textColor="@drawable/global_button_txt_selector_3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rv_channel" />
</android.support.constraint.ConstraintLayout>

And the result will be

real device

And the layout inspector shows

layout inspector

I have two problems

  1. When this code run on a real device the corresponding TexView's width became zero and it is not visible anymore. But when I removed the chain It is working as it is.

  2. It is based on the RecyclerView if it's height given match_contraints value it will shrink to the one cell height irrespective of how many items are there, but when I gave match_parent, it is wrapped to the items presented in it.

Please not that this view is inflated in DialogFragment

Here is my stackoverflow link