27

When using fill_parent, maxWidth has no effect.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
        <EditText android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:maxWidth="50dip"/>
    </LinearLayout>    
</LinearLayout>
Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
Evgeny Borzenkov
  • 1,107
  • 3
  • 12
  • 19

3 Answers3

33

The attribute maxWidth has no effect on a width of match_parent (or the deprecated fill_parent), they are mutually exclusive. You need to use wrap_content.

Also any layout that only has one child can probably be removed, for instance you can simplify you current layout to:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:maxWidth="50dip"/>
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 2
    Thank you! How to fill my EditText all width of the screen but not nore 500dip? For example some screen has resolution < 500. – Evgeny Borzenkov Nov 10 '12 at 19:59
  • 16
    This seems silly but it works: add `minWidth` and set both "min" and "max" to `500dp`. – Sam Nov 10 '12 at 20:18
  • 1
    @Sam - this doesnt seem to work for me, the editText still fills the entire screen except for the margins. Could you elaborate? – katzenhut Feb 27 '14 at 09:51
  • You can achieve the desired behaviour using a ConstraintLayout. See [this answer](https://stackoverflow.com/a/56060322/8302395). – Roland van der Linden Nov 14 '19 at 12:05
30

If anyone wants the combined behaviour of match_parent and maxWidth in a single parent layout: You can use a ConstraintLayout and constrain the child by the parent bounds combined with layout_constraintWidth_max.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText 
        android:layout_width="0dp"
        android:layout_height="wrap_content"                
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintWidth_max="480dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
8

You can achieve the same effect using different layouts for different screens. Say you want your EditText to fill the available width but not more than 480dp. Difine your layouts as follows:

layout/my_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>

layout-w480dp/my_layout.xml: (choose you max width as the qualifier here)

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="480dp"
          android:layout_height="wrap_content"/>
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148