How to use android:maxWidth?

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.


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.

Tags:

Android

Width