Skip to content Skip to sidebar Skip to footer

Change Height Percentage Based On Another View's Height In Constraintlayout

I have a view defined as

Solution 1:

You can divide the space 80%/20% by using "weighted chains" and ConstraintLayout. The idea is to take the two views you want to occupy a space based on percentages, set contraints to MATCH_CONSTRAINTS and set the weights appropriately using app:layout_constraintVertical_weight as follows. I have given background colors to views so they appear on screen. You will also want to consider setting the views to Space.

enter image description here

activity_main

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/signupBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/balanceText"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_marginTop="16dp"
        android:text="Balance text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signupBtn" />

    <View
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/guideLayout"
        app:layout_constraintEnd_toEndOf="@id/signupBtn"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@id/signupBtn"
        app:layout_constraintTop_toTopOf="@id/signupBtn"
        app:layout_constraintVertical_weight="0.2" />

    <View
        android:id="@+id/guideLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toBottomOf="@id/balanceText"
        app:layout_constraintEnd_toEndOf="@id/signupBtn"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@id/signupBtn"
        app:layout_constraintTop_toBottomOf="@+id/space"
        app:layout_constraintVertical_weight="0.8" />

</android.support.constraint.ConstraintLayout>

Post a Comment for "Change Height Percentage Based On Another View's Height In Constraintlayout"