4

I have an XML file which contains an EditText with these attributes:

<EditText
        android:id="@+id/et_firstname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/view_margin_below"
        android:hint="@string/et_firstname"
        android:maxLength="20"
        android:minLines="1"
        android:maxLines="1" />

Since android:singleline is deprecated, I looked for another option and I found these attributes, but they aren't working. I am allowed to enter as many newlines as I want.

Fogarasi Norbert
  • 650
  • 2
  • 14
  • 34

2 Answers2

7

Like described in API for minLinesand maxLines attribute, you have to use the attribute android:inputType="textMultiLine" too:

API:

android:maxLines

Makes the TextView be at most this many lines tall. When used on an editable text, the inputType attribute's value must be combined with the textMultiLine flag for the maxLines attribute to apply.

android:minLines

Makes the TextView be at least this many lines tall. When used on an editable text, the inputType attribute's value must be combined with the textMultiLine flag for the minLines attribute to apply.

<EditText
        android:id="@+id/et_firstname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/view_margin_below"
        android:hint="@string/et_firstname"
        android:maxLength="20"
        android:minLines="1"
        android:inputType="textMultiLine"
        android:maxLines="1" />

So I guess, You just have to add this attribute.

EDIT

To avoid misunderstandings: The behaviour of these attributes will allow to enter multiple lines, but it will be only shown one line. BUT, it´s scrollable, so if you put in some more lines, you can scroll up and down to see the other lines.

This is stated in API for singleLine. minLinesand maxLines work similar:

Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Meanwhile i figured out: the `inputType` attribute has to be `text`, not `textMultiLine` but you were close. Thanks. – Fogarasi Norbert Jul 11 '17 at 18:53
  • 1
    Ok, I made an Edit, because of the single line behaviour. I think it was a misunderstanding of the attributes. Admittetly, the API is a little bit misleading with the description "single line" . – Opiatefuchs Jul 11 '17 at 19:00
  • `singleLine=true` has a different behaviour, than `lines=1`, but in simple cases they work similarly. Sometimes I have to use `singleLine` in addition. – CoolMind Apr 16 '19 at 09:35
2

Since you defined both minLines and maxLines as 1, you may as well use lines="1" attribute.

  • Not working. I tried with only the `android:maxLines` attribute, same behaviour. – Fogarasi Norbert Jul 11 '17 at 18:35
  • @FogarasiNorbert try lines attribute, not maxLines. –  Jul 11 '17 at 18:36
  • @FogarasiNorbert try removing both minLines and maxLines attributes then. I believe EditText should not allow second line by its default behavior. –  Jul 11 '17 at 18:39