Home > Net >  layout_constraintHorizontal_weight=1 is not working in ConstraintLayout
layout_constraintHorizontal_weight=1 is not working in ConstraintLayout

Time:05-16

I have used layout_constraintHorizontal_weight=1 in three constraintLayout to make them equally divide horizontally. But it is not working. Why it is not taking in my code I am not getting. at the same time I need to make the contents of three constraintLayout center. xml code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@ id/clContent"
        android:layout_width="match_parent"
        android:layout_height="81dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@ id/clLeft"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_weight="1"
            android:layout_marginEnd="@dimen/dp_1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceBody2"
            android:text="Name"
            android:textColor="@color/primaryDark"
            android:layout_marginTop="@dimen/dp_8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvName">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvNameValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceHeadline2"
            android:textSize="@dimen/text_size_16"
            android:text="Rahul"
            android:textColor="@color/primaryDark"
            android:layout_marginTop="@dimen/dp_1"
            android:layout_marginBottom="@dimen/dp_4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@ id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?attr/textAppearanceBody2"
                android:text="Deb"
                android:layout_marginTop="@dimen/dp_5"
                android:textColor="@color/primaryDark"
                app:layout_constraintStart_toEndOf="@id/tvNameValue"
                app:layout_constraintTop_toTopOf="parent"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@ id/viewDividerLeft"
        android:layout_width="0.5dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider_blue"
        app:layout_constraintStart_toEndOf="@ id/clLeft"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/clMiddle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginStart="@dimen/dp_1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@ id/viewDividerLeft">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvRollNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceBody2"
            android:text="Roll No"
            android:textColor="@color/primaryDark"
            android:layout_marginTop="@dimen/dp_8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvRollNoValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceHeadline2"
            android:textSize="@dimen/text_size_16"
            android:text="323"
            android:textColor="@color/primaryDark"
            android:layout_marginTop="@dimen/dp_1"
            android:layout_marginBottom="@dimen/dp_4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvRollNo"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

        <View
            android:id="@ id/viewDividerRight"
            android:layout_width="0.5dp"
            android:layout_height="wrap_content"
            android:background="@drawable/divider_blue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@ id/clMiddle"/>


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@ id/clRight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_weight="1"
            android:layout_marginStart="@dimen/dp_1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@ id/viewDividerRight">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@ id/tvAddress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?attr/textAppearanceBody2"
                android:text="Address"
                android:textColor="@color/primaryDark"
                android:layout_marginTop="@dimen/dp_8"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@ id/tvAddressValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?attr/textAppearanceHeadline4"
                android:text="New Delhi"
                android:textColor="@color/primaryDark"
                android:layout_marginTop="@dimen/dp_1"
                android:layout_marginBottom="@dimen/dp_4"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvAddress"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

This the code which I am working.

CodePudding user response:

  1. For making app:layout_constraintHorizontal_weight work, all target Views have to squeeze each other:

    parent |<- A ->| |<- B ->| |<- C ->| parent
    
  2. If you do like above, the default effect is 1 : 1 : 1, so you actually don't need the app:layout_constraintHorizontal_weight, unless you want other weight ratio.

CodePudding user response:

Make in this way.

So when you give left side and right side for all views in constraintLayout with width=0, they stretch equally

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/clContent"
    android:layout_width="match_parent"
    android:layout_height="81dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/clLeft"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/dp_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/clMiddle"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_8"
            android:text="Name"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="@color/primaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvName">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@ id/tvNameValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_1"
                android:layout_marginBottom="@dimen/dp_4"
                android:text="Rahul"
                android:textAppearance="?attr/textAppearanceHeadline2"
                android:textColor="@color/primaryDark"
                android:textSize="@dimen/text_size_16"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@ id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_5"
                android:text="Deb"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textColor="@color/primaryDark"
                app:layout_constraintStart_toEndOf="@id/tvNameValue"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@ id/viewDividerLeft"
        android:layout_width="0.5dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider_blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@ id/clLeft" />
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/clMiddle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_1"
        app:layout_constraintLeft_toRightOf="@id/clLeft"
        app:layout_constraintRight_toLeftOf="@id/clRight"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvRollNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_8"
            android:text="Roll No"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="@color/primaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvRollNoValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_1"
            android:layout_marginBottom="@dimen/dp_4"
            android:text="323"
            android:textAppearance="?attr/textAppearanceHeadline2"
            android:textColor="@color/primaryDark"
            android:textSize="@dimen/text_size_16"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvRollNo" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@ id/viewDividerRight"
        android:layout_width="0.5dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider_blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@ id/clMiddle" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/clRight"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_1"
        app:layout_constraintLeft_toRightOf="@id/clMiddle"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_8"
            android:text="Address"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="@color/primaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvAddressValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_1"
            android:layout_marginBottom="@dimen/dp_4"
            android:text="New Delhi"
            android:textAppearance="?attr/textAppearanceHeadline4"
            android:textColor="@color/primaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvAddress" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  • Related