69

singleLine is/was used in xml layout files for TextView and EditText something like the following:

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

Some people on SO say singleLine is deprecated, while other people still suggest using it. Sometimes it even seems necessary to use when maxLines="1" doesn't work. (see here, here, and here)

The docs should be the place to go to answer this question, right? Here, they say:

This constant was deprecated in API level 3.

This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

However, in the TextView docs, there is no indication that it is deprecated, either for android:singleLine or for setSingleLine or for setTransformationMethod. The same TextView docs, by comparison, do state that other things like STATUS_BAR_HIDDEN and fitSystemWindows are deprecated. So is the singleLine deprecation an omission, was it "undeprecated", or what?

This question has been previously asked before but was not the main focus of the question (and was not answered).

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • In which cases _maxLines="1"_ doesn't work ? – JafarKhQ May 04 '15 at 11:29
  • can you post the `code` block for both the cases? – Maveňツ May 04 '15 at 11:57
  • 1
    I'm not really asking here why a particular instance of `maxLines="1"` supposedly doesn't work. (It could be that it really does work in every instance.) I just want to know if `singleLine` is really deprecated or not. That being said, I don't have any authoritative examples but see the `ellipsize="marquee"` part of [this answer](http://stackoverflow.com/a/30029722/3681880) and [this Q&A](http://stackoverflow.com/questions/24351698/textviews-ellipsize-not-working-on-maxlines-1) and also [this answer with comments](http://stackoverflow.com/a/10748836/3681880). – Suragch May 04 '15 at 12:24
  • I faced a strange side effect of singleLine attribute - I used `TextView`s with it in `ViewPager` and the pager did not respond to touch slide events, if I slided with finger over that `TextView`s. But when I changed that attribute to maxLines=1, everything went ok! – schmidt9 Jul 23 '16 at 06:07
  • @JafarKhQ There are some cases where it behaves inferiorly, particularly with ellipsizing. – Learn OpenGL ES Nov 11 '16 at 16:56

10 Answers10

49

I think the answer to your question is already in one of the SO posts you linked to. Unfortunately, the deprecation of singleLines is not a black-or-white matter.

It is deprecated, but it is not going anywhere anytime soon.

It was deprecated because its performance is poor, relative to its successor, maxLines. It uses SingleLineTransformationMethod to replace newlines and carriage returns in the String you place in the TextView, unlike maxLines, which "just" adjusts the height of the TextView based on the number of lines and does no String replacing.

This method of replacing characters also meant that singleLine could break in unexpected ways (e.g. if you use custom fonts). It was these performance and reliability issues that led to its deprecation.

However, it is not going anywhere because, as the SO post you linked to states, it is still in use by many old Android applications, and it is still useful sometimes (e.g. when you want to show the entire text on one line and ignore carriage-returns and newlines).

Do note that deprecation does not necessarily mean that an API is going away. It just means that its use is discouraged, but may be permitted.

Community
  • 1
  • 1
ugo
  • 2,705
  • 2
  • 30
  • 34
  • 3
    If it is actually deprecated, then isn't it the official practice to mark it as so throughout the documentation? Yet the TextView docs don't do that. That's what makes me wonder whether this is an oversight or if it is being purposefully vague or something else. – Suragch May 19 '15 at 01:34
  • 1
    @Suragch yes, it is likely an oversight (it wouldn't be the first oversight in Android docs). Keep in mind that it was deprecated very early on in Android's development. In fact, the [git commit](https://github.com/android/platform_frameworks_base/blob/d24b8183b93e781080b2c16c487e60d51c12da31/core/res/res/values/attrs.xml#L1701) that deprecated it is one of the first ones in Github's import of Android source and is dated April 2009. It was deprecated very early on in development, but not all docs were updated. – ugo May 19 '15 at 02:50
  • 2
    I'm not sure why the documentation would recommend using `maxLines`. It does not supply the same behavior as `singleLine`. – Eliezer Feb 09 '17 at 22:05
21

The deprecated attribute was added in change d24b8183b9 which is nothing but a dump from Google's internal SCM:

auto import from //branches/cupcake/...@130745

As can be seen from the change core/res/res/values/attrs.xml diff adds the @deprecated doc comment, but core/java/android/widget/TextView.java diff does not alter anything from setSingleLine()'s doc comment.

Now without access to Google's internal SCM history, it is not possible to know what exactly caused above change in attrs.xml doc comment, but for your question

So is the singleLine deprecation an omission, was it "undeprecated", or what?

one possible answer is:TextView's single-line was neither deprecated, nor "undeprecated", but it was enhanced to take into account whether the view is editable, a password field or uses any other input type flag that affects single/multi-lineness.

ozbek
  • 20,955
  • 5
  • 61
  • 84
  • 2
    Several of the answers here provide some useful discussion and insight into this problem, but since the bounty time is almost up, I am awarding it to this answer because it goes into the most depth and draws the most from credible and official sources. I'm not quite ready to choose an accepted answer, yet, though. – Suragch May 19 '15 at 13:38
11

Just for adding some more information to the discussion, Lint now has the following error:

"Combining ellipsize and maxLines=1 can lead to crashes on some devices. Earlier versions of lint recommended replacing singleLine=true with maxLines=1 but that should not be done when using ellipsize.

More info: https://issuetracker.google.com/issues/36950033"

So, I guess that singleLine is now, and I think we should come up with a new term... "deprecatedish"?

Rui
  • 936
  • 10
  • 21
  • 1
    This comment is 4 years old. FOUR years ago this was "deprecatedish" and unclear, and now it still ist. This is insanity. – avalancha Nov 11 '21 at 12:11
9

In the official grepcode of TextView (v5.1.0 r1) :

android:singleLine is not annotated with @Deprecated.

I also see this in setInputType method:

boolean singleLine = !isMultilineInputType(type);

// We need to update the single line mode if it has changed or we
// were previously in password mode.
if (mSingleLine != singleLine || forceUpdate) {
    // Change single line mode, but only change the transformation if
    // we are not in password mode.
    applySingleLine(singleLine, !isPassword, true);
}

setInputType overrides the mSingleLine value so.

EDIT : This xml attribute is now officially deprecated. (since API 3?). It is now visible in AndroidStudio xml editor.

Täg
  • 431
  • 4
  • 17
5

Just thought I'll add that Android Studio 2.2.1 flags singleLine as deprecated. However, what I found is that in my instance:

android:singleLine="false"

works well, whereas

android:maxLines="2"

does not.

Seb
  • 81
  • 1
  • 3
5

While Android Studio claims that it is deprecated, in reality we had an edit text which should allow itself to be only single-line.

Adding maxLines="1" made it allow newline characters, which is not suitable for our needs.

So we went back to using singleLine="true".

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
5

singleLine IS deprecated. No discussion needed.

The only problem is the wrong documentation.
"use the textMultiLine flag in the inputType attribute instead for editable text views" is the direct opposite of what you want to achieve by using singleLine.

Instead of that you can use android:inputType="text".
For me it did exactly what I wanted - an edit with a single line without line breaks.

The incredible Jan
  • 741
  • 10
  • 17
  • The OP described why discussion is needed. Declaring "no discussion needed" without citing a credible source as to why the issue is closed doesn't help. – LarsH Apr 17 '19 at 21:04
5

I have just added android:inputType="text"and removed android:maxLines="1", it worked fine for me.

Divya
  • 121
  • 2
  • 11
1

Only providing android:maxLines="1" and android:minLines="1" wont solve the issue of keyboard actionNext related issue. Use android:inputType="text" for the same.

sid8491
  • 6,622
  • 6
  • 38
  • 64
oj24031989
  • 31
  • 1
  • 4
0

Simple alternate way, use: android:maxLines="1"