Android Constraintlayout Set Width or Height Percent Programatically

app:layout_constraintHeight_percent
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/previewLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_height="match_parent"
    tools:layout_width="match_parent"
    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"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/contentTextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:gravity="center"
        app:autoSizeMaxTextSize="36sp"
        app:autoSizeStepGranularity="4sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="当你觉得别人的生活是完美时,那是因为你不够了解他人。" />

</androidx.constraintlayout.widget.ConstraintLayout>

Adjust app:layout_constraintHeight_percent programatically.

contentTextView.updateLayoutParams<ConstraintLayout.LayoutParams> {    matchConstraintPercentHeight = 0.6f}

❤️ 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.