Search code examples
androidxmlviewalignment

Android Studio 3.62 - aligning views in the xml file


I have an EditText-element and a Button-element and I would like to have the editText above the button.
And then I would like to have their left sides aligned.
So in the code below the editText is centered for some reason and that is ok, but I don't want the button to be centered, the button should start at the same x-position as the editText because there will be another button in the same row.

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:hint="@string/input_msg"
        android:importantForAutofill="no"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/insert_msg"
        app:layout_constraintTop_toBottomOf="@+id/editText"/>

Solution

  • The solution was to use the constraintStart and constraintEnd and connect it to the constraintStart and constraintEnd of the EditText like in the code below:

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:hint="@string/edittexthint"
            android:importantForAutofill="no"/>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="@string/insert_msg"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            app:layout_constraintStart_toStartOf="@+id/editText"/>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="@string/delete_msg"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            app:layout_constraintEnd_toEndOf="@+id/editText"/>