32

I want to set a maximum width of an edit box. So I created this small layout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:maxWidth="150dp" > 

    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:ems="10" /> 
</LinearLayout> 

But it doesn't work, the box could be more than 150 dp anyway. android:maxWidth="150dp" in the EditText will give the same result. I have read ( maxWidth doesn't work with fill_parent ) that setting both max- and min width to the same size should solve it:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:maxWidth="50dp"
        android:minWidth="50dp" />

But it doesn't.

I have found a solution to my problem here here: Setting a maximum width on a ViewGroup And this works great. But I guess the maxWidth attribute is here for I reason. How should it be used? Or is there any documentation on this? I have spend hours on this problem, and still doesn't understand how to use this simple attribute.

Community
  • 1
  • 1
PEK
  • 3,688
  • 2
  • 31
  • 49

2 Answers2

37

I want to set a maximum width of an edit box.

In your example:

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

The layout_width and ems attributes are trying to set the same value. Android seems to choose the larger fill_parent value and ignores the other. And when you use this:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:maxWidth="50dp"
    android:minWidth="50dp" />

Here ems and maxWidth are trying to set the same value, again the greater value is used. So depending on what you actually want you can use:

<EditText 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

or replace android:ems="10" with android:maxWidth="50dp" if you prefer to work with dp.

Lastly your LinearLayout only has one child, the EditText. Typically when this happens you can remove the LinearLayout tags and use the EditText by itself.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks, that is how expected this to work more or less :-). But it doesn't :-(. If I change my LinearLayout tag to yours I still get the width on the edit box, no matter which value I put on the maxWidth attribute :-(. I'm really confused. – PEK Dec 06 '12 at 21:10
  • I misread your question, so I redid my answer. How big of a device are you working with? – Sam Dec 06 '12 at 22:04
  • Frankly speaking I didn't know what the ems attribute was for, I wish I would have checked that before I asked my question :-). To solve my problem I should: Remove the ems-tag, set android:layout_width to "wrap_content" and set both minWidth and maxWidth to the maximum width. This raises another question: what is the purpose of minWidth? If I don't set a value to minWidth, the maxWidth attribute seems to be ignored. If I set a value to minWidth that is less than maxWidth, then minWidth is used. My problem is solved, I'm a bit wiser but still a bit confused of all these attributes :-). – PEK Dec 07 '12 at 16:53
  • 2
    From what I've seen `minWidth` and `layout_width="wrap_content"` (and `ems`) all set the basic value, it's similar to `maxWidth` but Android chooses the _smallest_ value among them. This is why if you set `layout_width="wrap_content"`, `maxWidth="500dp"`, and `minWidth="500dp"` the layout will be 500dp wide unless the screen is smaller than 500dp. – Sam Dec 07 '12 at 17:51
34

To those who are seeing this question in recent years, we have app:layout_constraintWidth_max="225dp" now. Put your layout inside contsraint layout and use this on the view you want to have max width.

yooneskh
  • 784
  • 7
  • 11