android – how to fix the size of multiline edit text-ThrowExceptions

Exception or error:

I want to make a multi line edit text but the problem is that whenever i enter new line height of edit text increases. how to fix its height. My code is

<EditText
    android:id="@+id/editAdditionalInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editPhoneCutomerDetails"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="60dp"
    android:layout_below="@+id/editPhoneCutomerDetails"
    android:layout_marginTop="10dp"
    android:background="@drawable/message_additional_box1x"
    android:ems="10"  
    android:padding="10dp"
    android:gravity="top">
    <requestFocus />
</EditText>
How to solve:

To fix the size of editText you can use

android:singleLine="true"

but it can limit your editText line to 1 .

if you want more lines in editText then use the following property.

Use android:lines="3" .

###

Use android:lines="2" for fixing multilines and for fixing single line use android:singleLine="true"

for example use like this as shown for multilines

<EditText
    android:id="@+id/.."
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:drawable/editbox_background_normal"
    android:ems="10"
    android:hint="Name"
    android:maxLength="20"
    android:lines="2"// fix the multiline limit 
    android:textColor="#000000" />

for example use like this as shown for single line

<EditText
    android:id="@+id/.."
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:drawable/editbox_background_normal"
    android:ems="10"
    android:hint="Name"
    android:maxLength="20"
    android:singleLine="true"// fix the single line limit 
    android:textColor="#000000" />

###

try this;

android:inputType=”textMultiLine”

 <EditText
        android:inputType="textMultiLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

###

As singleline is now deprecated use inputType:multiline and set the number of lines you want with lines tag

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:lines="4"/>

###

I had a similar requirement but ended up not changing the size of the box. i.e it grows as text is typed into it but stops short of blocking the other widgets in view. I read this post during my quest and had found a solution. Decided to post it back here. Restricting size to a value may possibly have different effects on different screens. So I think it is better to let it be.

The thing is to put the edit text inside a linearlayout with the rest of the widgets. The code and snapshot are here
Multi line edit box with ok cancel buttons

Leave a Reply

Your email address will not be published. Required fields are marked *