15

I have an EditText which I want to fill all the horizontal width available but the width should not be greater than 200dp

This makes EditText adapt to any screen size and still makes it look good on large screens (stretching horizontally won't look nice on a large screen).

How to do this in Android?

I saw that maxWidth=200dp and layoutWidth=fill_parent do not work together:

 <EditText android:id="@+id/oldpassword"
           android:hint="@string/youroldpassword"
           android:inputType="textpassword"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:maxWidth="250dp" />

If maxWidth and layoutWidth=fill_parent do not work together, then what is the maxWidth meant for?

In other words, if the EditBox does not change its width dynamically, then what would you need maxWidth for?

WriteEatSleepRepeat
  • 3,083
  • 3
  • 33
  • 56

6 Answers6

4

you can set width programmatically depending on the device screen size. like

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
EditText et = (EditText)findViewById(R.id.editText);
if(width > 200)
{
    et.setWidth(200);
}
else
{
    et.setWidth(width);
}
swati srivastav
  • 635
  • 4
  • 15
  • 3
    I was afraid about that. Is this such an uncommon thing to achieve? Also, I am confused about what maxWidth is meant for. If it doesn't work with fill_parent, then what is it for? – WriteEatSleepRepeat Sep 18 '13 at 07:38
  • as you require to set it dynamically so i thing not possible to work on xml files. ok still m trying for alternate option. wait... – swati srivastav Sep 18 '13 at 07:41
2

i have solution, look if you set android:layout_width="fill_parent" it was always having it's width which have parent but when you set android:layout_width="wrap_content" then it was increase size width with content while you entering Text in EditText, now if you use android:maxWidth="250dp" with android:layout_width="wrap_content" then it will increase it's width upto 250dp while entering value in EditText

use

<EditText android:id="@+id/oldpassword"
       android:hint="@string/youroldpassword"
       android:inputType="textpassword"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:maxWidth="250dp" />`

or use

<EditText android:id="@+id/oldpassword"
       android:hint="@string/youroldpassword"
       android:inputType="textpassword"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
Rajesh
  • 2,618
  • 19
  • 25
  • I need something in between. I want the EditText to fill the parent but its width should not be over 250dp. But your explanation is useful, thanks – WriteEatSleepRepeat Sep 18 '13 at 10:12
  • then you can define layout_width="250dp" fix other then no solution may be any one can help. – Rajesh Sep 18 '13 at 10:38
1

remove maxWidth=200dp in your layout

Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
1

You can wrap edit text to relative layout.Here is what you can do -:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/oldpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="250dp"
        android:maxLines="1"
        />
</RelativeLayout>
Abhishek Birdawade
  • 301
  • 1
  • 2
  • 14
-1

You can wrap the editText in layout(which have width of 200dp) and make editText to match_parent.

Evgeni Roitburg
  • 1,958
  • 21
  • 33
-3

I solved it using android:layout_marginRight="300dp", the greater is this value, the smaller is the width of the EditText object's line.

  • It's not the solution, because Android support 8000+ devices and 300 dp it's very big and you're layout should be very useless on some devices – Hugo Mallet Dec 11 '15 at 05:54