androidxmltablelayout

EditText width is larger than its parent TableRow


I have couple of TableRows, each with a TextView and an EditText inside. But the EditText doesn't fit the table row, even though I've set its width to match_parent. The EditText ends outside the TableRow. I want it to fit the TableRow width.

Here's an image of what I mean:

enter image description here

How can I make it so the EditTexts end here, where it's red?:

enter image description here

Here's my xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    tools:context=".activity.ConfigScreenActivity">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/novo_usuario_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/novo_usuario_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/novo_usuario_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Novo Usuário"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="TextContrastCheck" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/nova_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/nova_senha_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/nova_senha_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPassword"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,TextContrastCheck" />
            </TableRow>

        </TableLayout>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/confirmar_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:singleLine="false"
                    android:text="@string/confirmar_senha_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/confirmar_senha_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp" />

            </TableRow>

        </TableLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:text="@string/confirmar_button" />

            <Button
                android:id="@+id/cancelar_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="changeToMainScreen"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:text="@string/cancelar_button" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You can solve this issue by assigning weight to your EditTexts

    Try that

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="32dp"
        android:orientation="vertical"
        tools:context=".activity.ConfigScreenActivity">
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/novo_usuario_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/novo_usuario_texto"
                    android:textSize="20sp" />
    
                <EditText
                    android:id="@+id/novo_usuario_EditText"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="Novo Usuário"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="TextContrastCheck" />
    
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/nova_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/nova_senha_texto"
                    android:textSize="20sp" />
    
                <EditText
                    android:id="@+id/nova_senha_EditText"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPassword"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,TextContrastCheck" />
            </TableRow>
    
        </TableLayout>
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/confirmar_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:singleLine="false"
                    android:text="@string/confirmar_senha_texto"
                    android:textSize="20sp" />
    
                <EditText
                    android:id="@+id/confirmar_senha_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp" />
    
            </TableRow>
    
        </TableLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:text="@string/confirmar_button" />
    
            <Button
                android:id="@+id/cancelar_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:onClick="changeToMainScreen"
                android:text="@string/cancelar_button" />
        </LinearLayout>
    </LinearLayout>