nverbeek
nverbeek

Reputation: 1892

ConstraintLayout Sizing Issue

I'm having an issue sizing children in a ConstraintLayout. I have 3 views:

If item_quantity has short text, I want it to look like the image below. I want data_entry_layout to fill all available space.

short text version

If item_quantity has long text, I want it to fill and grow but I want the data_entry_layout to never be smaller than 120dp. I want it to honor the minWidth. So, I want it to look like this:

long text version

Here is my current configuration:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <TextView
        android:id="@+id/item_quantity"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/data_entry_layout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="90,000 Boxes of Stuff Are Needed"
        android:textColor="@color/color_on_background"
        android:textSize="@dimen/workflow_item_view_item_quantity_text_size" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/data_entry_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/camera_scan"
        app:layout_constraintStart_toEndOf="@id/item_quantity"
        app:layout_constraintVertical_chainStyle="spread_inside"
        android:contentDescription="@string/disable_realwear_asr"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:minWidth="120dp"
        app:errorIconDrawable="@null">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/data_entry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/activity_instruction_data_entry_text_size"/>
    </com.google.android.material.textfield.TextInputLayout>

    <ImageView
        android:id="@+id/camera_scan"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/data_entry_layout"
        app:layout_constraintTop_toTopOf="@id/data_entry_layout"
        android:background="@android:color/transparent"
        android:clickable="true"
        android:foreground="@drawable/ripple_selector"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:src="@drawable/qrcode_scan"
        android:visibility="visible"
        tools:ignore="ContentDescription,KeyboardInaccessibleWidget" />
</androidx.constraintlayout.widget.ConstraintLayout>

If I set item_quantity's layout_width to 0dp then the UI works with lots of text, but takes up too much horizontal space with short text:

0dp looks bad with short text

If I set item_quantity's layout_width to wrap_content then the UI works with short text, but pushes the data_entry_layout off the screen with large text:

wrap_content looks bad with long text

I must be missing something simple. Is there a way I can get both of these situations to do what I want?

Upvotes: 0

Views: 171

Answers (1)

Meefle
Meefle

Reputation: 88

Rather than use the android:minWidth property try using app:layout_constraintWidth_min. ConstraintLayout seems to either ignore or overrule certain properties with the "android" namespace when it comes to sizing of elements.

Upvotes: 3

Related Questions