Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make an EditText have a maximum width but still fill parent width

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?

like image 532
WriteEatSleepRepeat Avatar asked Sep 18 '13 07:09

WriteEatSleepRepeat


2 Answers

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);
}
like image 66
swati srivastav Avatar answered Oct 25 '22 06:10

swati srivastav


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"/>
like image 24
Rajesh Avatar answered Oct 25 '22 06:10

Rajesh