68

I recently used TextInputEditText and I got lint error that singleLine attribute is Deprecated

<android.support.design.widget.TextInputEditText
            android:id="@+id/my_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/string_hint_dob"
            android:lines="5"/>
</android.support.design.widget.TextInputLayout>

Getting strike-through as below:

enter image description here

Is there any alternative way for this?

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

6 Answers6

67

The android:singleLine attribute has been deprecated since API Level 15. You can achieve the same behaviour by using android:maxLines, which allows you to specify an arbitrary number of lines. This is superior to android:singleLine, which restricts you to only allowing one line.

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:minLines="2"
     android:maxLines="2" /> <!-- can specify arbitrary number of max lines -->
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
fractalwrench
  • 4,028
  • 7
  • 33
  • 49
51

Always define input type for single line

ex : inputType="text"

You don't need to do anything else.

Arihant
  • 660
  • 6
  • 9
39

android:singleLine is deprecated since API 3, you have to use android:maxLines instead (in your case android:maxLines="1").

The reason of the deprecation is for its bad performance. Anyway the singleLine attribute will not be removed because it's still the only way to make some effects that android:maxLines can't make:

e.g.

This will produce a scrolling horizontal text on one line if the text is selected.

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:ellipsize="end"
     android:scrollHorizontally="true" />

Instead, this won't work:

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:maxLines="1"
     android:ellipsize="end"
     android:scrollHorizontally="true" />
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • 12
    Also, android:singleLine="true" allows to check for newline characters and move to next field. android:maxLines="1" does not. – DrMad Oct 11 '16 at 04:18
  • 1
    Exactly I agree with you Fondesa. But is there any alternate for this?? – MashukKhan Mar 15 '17 at 14:07
  • @MashukKhan I didn't find it, if you find a way to do it, tell me, I'll update my answer for other users. – Giorgio Antonioli Mar 15 '17 at 16:00
  • `This will produce a scrolling horizontal text on one line if the text is selected` it's not working for me even I did `android:singleLine="true" android:ellipsize="end" android:scrollHorizontally="true"` – Shailendra Madda Jun 07 '18 at 04:17
21
<EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="text"
      android:maxLines="1"
      />

use android:inputType="text" and android:maxLines="1" togather

Ketan Ramani
  • 4,874
  • 37
  • 42
6

Following a commment of Juan José Melero Gómez, I would like to add more information:

If you are attempting to set an imeAction to your EditText such as actionSearch, setting android:maxLines="1" won't be enough. Adding android:inputType="text" is mandatory if you want to see the search icon.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
0

Needed replace android:singleLine="true" to android:maxLines="1" if you want indicate imeOptions needed add this line android:inputType="number|text ..."