Constraint Layout put view below if there is not enough space horizontally

Viewed 37

Is anyone aware of how to position the buttons vertically if the button text is too long inside of the xml?

I want the buttons to be positioned horizontally if they fit, if one of the buttons has to hyphenate, then I would like to avoid that by positioning the buttons below each other. Is there a way to do it inside of the XML only?

Thanks!

Layout where button text is too long

Expected behavior

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_header"
        style="@style/TitleTextAppearance.WithPrimaryBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/header_enter_startcode" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_background_with_margin">

        <TextView
            android:id="@+id/tv_status"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/pls_enter_startcode"
            android:textSize="@dimen/text_size_default"
            android:textAlignment="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til_startcode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/grid_2"
            app:helperText="@string/example_startcode"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_status">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/tiet_startcode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text"
                android:maxLines="1"
                android:textStyle="bold" />

        </com.google.android.material.textfield.TextInputLayout>

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/gl_middle"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintGuide_percent="0.5"
            app:layout_constraintStart_toStartOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_continue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:layout_marginEnd="@dimen/grid_2"
            android:text="@string/btn_continue"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/gl_middle"
            app:layout_constraintTop_toBottomOf="@+id/til_startcode" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_cancel"
            style="?materialButtonOutlinedStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:layout_marginEnd="@dimen/grid_2"
            android:text="@string/cancel"
            app:layout_constraintEnd_toStartOf="@+id/gl_middle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/til_startcode" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
0 Answers
Related