3

Hi i need to restrict the EditText to two lines and i am using the following code

<EditText
android:layout_toRightOf="@+id/valettext"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="70dp"
android:maxLines="2"
android:ellipsize="end"
android:id="@+id/note"/>

but its not working ,its going to the next line on pressing the Next button.

How to do this.

Thanks in advance.

surendra
  • 2,217
  • 7
  • 34
  • 42
  • Duplicated http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set – MarkB Sep 27 '11 at 10:31
  • Please check my code here... http://stackoverflow.com/questions/8911638/set-maximum-number-of-text-lines-for-an-edittext/23310236#23310236 – Jay Nair Apr 26 '14 at 11:36

3 Answers3

2

override your edittext's addaddTextChangedListener() and inside afterTextChanged() method try this code.

 @Override
 public void afterTextChanged(Editable editable) {
    if (null != edittext.getLayout() && edittext.getLayout().getLineCount() > 2)  {
       edittext.getText().delete(edittext.getText().length() - 1, edittext.getText().length());
    }
 }
2

You need to add this:

android:singleLine="false"

Also, set the number of lines prior to display:

android:lines="2"
SBerg413
  • 14,515
  • 6
  • 62
  • 88
  • This shows user two lines but when a third one is inputed, the EditText is expanded. See my solution: http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set/7103931 – Indrek Kõue Nov 09 '11 at 04:27
0
<EditText
   android:id="@+id/edit_text"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="text"
   android:maxLines="1"  />

You just need to make sure you have the attribute "inputType" set. It doesn't work without this line.

android:inputType="text"
Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32