0

I have a ConstraintLayout that contains a ViewStub with the app:constraintHeight_default property set to spread in XML. I need to be able to update this property to have a value of wrap at runtime, but so far nothing has worked for me. Here is the layout:

<?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"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    >

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_view"
        app:layout_constraintTop_toBottomOf="@+id/top_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="spread"
        />

    <com.example.TopView
        android:id="@+id/top_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/view_stub"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="wrap"
        />

    <com.example.BottomView
        android:id="@+id/bottom_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fields_stub"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="wrap"
        />

So far, I've tried using a ConstraintSet to set constraintHeight_default programmatically, but the layout inflated from the ViewStub ends up having 0 width and 0 height, so it isn't rendered in the layout.

ConstraintLayout constraintLayout = root.findViewById(R.id.constraint_layout);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
constraintSet.applyTo(constraintLayout);

ViewStub viewStub = root.findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.custom_layout);
viewStub.inflate();

How can I set the height of the ViewStub to wrap its height programmatically?

1 Answers1

1

Try changing the following line:

constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);

to constrainDefaultHeight

constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);

See the ConstraintSet documentation.

constrainDefaultHeight

void constrainDefaultHeight (int viewId, int height)

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131