Android Update ContraintLayout Constraint Programtically

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}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.