Hong
Hong

Reputation: 18531

Why does attribute maxWidth not work for EditText?

It seems that maxWidth has no effect on EditText. Could anyone shed some light on this?

<EditText
    android:id="@+id/editTextFoo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autofillHints="port"
    android:ems="10"
    android:maxWidth="100dp"
    tools:targetApi="o" />

The above code results in the following: enter image description here

If I set layout_width="100dp" as following:

<EditText
    android:id="@+id/editTextFoo"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:autofillHints="port"
    android:ems="10"
    android:maxWidth="100dp"
    tools:targetApi="o" />

the width is correct: enter image description here

Upvotes: 0

Views: 127

Answers (1)

MehranB
MehranB

Reputation: 1525

Yes I just checked in the IDE and it's because ems="10" is preventing the view from maintaining maxWidth 100dp. Apparently ems of 10 requires a larger width so it is overriding the maxWidth attribute.

Instead of ems go with minWidth to prevent the view from shrinking.

Alternatively, you can use maxEms and minEms.

Upvotes: 1

Related Questions