Android Update ContraintLayout Constraint Programtically

July 25, 2018
Android KTX updateLayoutParams

In the following xml, contentTextView is app:layout_constraintBottom_toBottomOf="parent".

<android.support.constraint.ConstraintLayout
    android:id="@+id/contentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.luasoftware.pixpin.lib.view.OutlineTextView
        android:id="@+id/contentTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

The following code update bottom constraint to top constraint using Android KTX.

contentTextView.updateLayoutParams<ConstraintLayout.LayoutParams> {
    topToTop = ConstraintLayout.LayoutParams.PARENT_ID
    bottomToBottom = ConstraintLayout.LayoutParams.UNSET
}

Update top constraint back to bottom constraint.

contentTextView.updateLayoutParams<ConstraintLayout.LayoutParams> {
    topToTop = ConstraintLayout.LayoutParams.UNSET
    bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.