7

I am new to ConstraintLayout. I have tried to set the TextInputLayout width to match parent but it's always jump to 365dp. And i can't able to align the TextInputLayout in bottom of another. Please help me with this issue . Screenshot

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dcastalia.com.cook4me.LoginActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">


<android.support.design.widget.TextInputLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="39dp"
    android:layout_marginLeft="32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="32dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginStart="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintHorizontal_bias="0.0"
    android:id="@+id/textInputLayout">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:layout_width="395dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
    app:layout_constraintRight_toRightOf="@+id/textInputLayout">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint" />
</android.support.design.widget.TextInputLayout>

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
Piash Sarker
  • 103
  • 1
  • 6

2 Answers2

4

If you want TextInputLayout width match_parent, you should set android:layout_width="0dp" and remove all marginStart and marginEnd (or marginLeft and marginRight)

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        />
</android.support.design.widget.TextInputLayout>
Linh
  • 57,942
  • 23
  • 262
  • 279
  • This is incorrect. You should never use `match_parent` with ConstraintLayout. – Martin Marconcini Jun 08 '18 at 19:56
  • I don't think that's true. What if you want it to take up the whole screen? – Marty Miller Nov 14 '18 at 02:24
  • @MartyMiller Thank you for pointing out my mistake. I have updated my answer – Linh Nov 14 '18 at 02:34
  • I think you're mixing view groups. The attributes width/height are there so a widget can *inform its parent* how much space it needs; whether the parent can accommodate for this or not, is a different story to be resolved after onMeasure + onLayout… so a ConstraintLayout *does not support* widgets telling it that they need to `MATCH_PARENT`; they can, however, say they want to match their constraints (`0dp`) and then let the algorithm decide how much space is available; a CL however, can tell *its* parent that it wants to `MATCH_PARENT`, provided the parent is *not* a CL itself! – Martin Marconcini Nov 14 '18 at 02:57
  • @MartyMiller if the CL wants to use the whole screen, that's the parent's problem, not the CL. **if a view inside the CL** wants to use the whole screen, the best it can do, is tell the CL: I want to use as much space as you can! (and it does so by using `0dp` and then pinning to the `parent` on all (Relevant) sides. In essence, a CL cannot *honor* MATCH_PARENT, but can *request* it to its own parent (again, assuming a CL is not inside another CL, because if that's the case, the "external" CL/Parent, will be in the same situation: "WTF IS MATCH_PARENT"? :) – Martin Marconcini Nov 14 '18 at 03:00
  • I see where you're coming from. I'm just thinking if you want a view in the CL to be at the bottom of the screen (constrain to bottom of parent) then the CL will have to reach the bottom of the screen using match_parent. Also if your layout root is a CL and you want it to have a background color that fills the whole screen, the CL will have to be match_parent. The app I'm currently working on has a FrameLayout (activity) with holds Fragments (CL) – Marty Miller Nov 15 '18 at 00:11
  • 1
    I had this problem with DialogFragment. This solution works for me. Thanks! – gopalanrc May 11 '20 at 09:35
0

Try this

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp"
    android:id="@+id/constraint">


    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout1"
        app:layout_constraintLeft_toLeftOf="@+id/textInputLayout1"
        app:layout_constraintRight_toRightOf="@+id/textInputLayout1">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint" />
    </android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>
S Haque
  • 6,881
  • 5
  • 29
  • 35