Pinkesh
Pinkesh

Reputation: 91

layout_constraintHorizontal_weight = 1 in ConstraintLayout is not Working

TextInputEditText in TextInputLayout equal width in ConstraintLayout using wight property. used layout_constraintHorizontal_weight = 1 is not work.how to give same width using weight property to Textinputlayout. This is give below result.

enter image description here

I want to below result :

enter image description here

xml code is :

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"


        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"

        app:layout_constraintHorizontal_weight="1"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toStartOf="@+id/tilLastName1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFirstName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:background="@drawable/custom_edittext_input"
            android:hint="First Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>



    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLastName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"

        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLastName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="Last Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 2069

Answers (2)

Brent K.
Brent K.

Reputation: 1085

I gave up on layout_constraintHorizontal_weight and instead use guidelines between the buttons.

Start by creating the guidelines within the ConstraintLayout.

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.50" />

Then you can constrain the TextInputs to that @id/guideline_center. By specifying a 0dp width it will fill the area to the guidelines.

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintEnd_toStartOf="@id/guideline_center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        ...

Upvotes: 0

Rafsanjani
Rafsanjani

Reputation: 5443

I made a few modifications to your code to end up with this. The trick it to constrain the end of the first firstName1 to the start of lastName1 and do the opposite for lastName1 and distribute the margins between them at the boundary.

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toStartOf="@+id/tilLastName1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFirstName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="First Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLastName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tilFirstName1"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLastName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="Last Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Related Questions