32

I thought singleLine="true" was equivalent to maxLines="1" but I see that the following pre-populated field from Android Studio has both. Is there a difference? Is there a known bug that causes both to be required?

<EditTextPreference
   android:key="example_text"
   android:title="@string/pref_title_display_name"
   android:defaultValue="@string/pref_default_display_name"
   android:selectAllOnFocus="true"
   android:inputType="textCapWords"
   android:capitalize="words"
   android:singleLine="true"
   android:maxLines="1" />

this is from the pref_general.xml file.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • 2
    Maybe related? http://stackoverflow.com/a/17125324/2095855 – Edward van Raak Jun 16 '15 at 22:41
  • Doesn't it have something to do with being able to add new lines with the keyboard? I'm pretty sure singleLine="true" blocks it from making another line. – Edward van Raak Jun 16 '15 at 22:53
  • As far as I know and atleast in the new version of Android Studio, the preferences XML file is done manually. So, I was wondering how did Android Studio pre-populate it when you do that yourself. – Christian Abella Jun 16 '15 at 23:24

2 Answers2

16

From Android website:

singleLine:

Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key. The default value is false (multi-line wrapped text mode) for non-editable text, but if you specify any value for inputType, the default is true (single-line input field mode).

Must be a boolean value, either "true" or "false".

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.

Must be an integer value, such as "100"

Please note that singleLine has been deprecated since API 3 and maxLines should be used instead. So all you need really is

android:maxLines = integer // 1 for single line or add lines multiple as well.
Kalimah
  • 11,217
  • 11
  • 43
  • 80
  • 4
    Is `singleLine` really deprecated? I found no warnings in the visual preview of AS. In my case `singleLine=true` and `maxLines=1` give different results. – akhy Jan 07 '16 at 03:32
  • 1
    singleLine does not appear to be deprecated. http://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine – Mike May 05 '16 at 20:27
  • 1
    It is now, but for EditTexts, `singleLine` should still be used because `maxLines` will not wrap carriage returns. – Henrique de Sousa Jun 23 '16 at 16:35
  • 7
    `maxLines=1` doesn't prevent user input linebreaks, but `singleLine` does. It doesn't make sense to use `maxLines` instead. – Kimi Chiu Jan 22 '17 at 11:39
  • you can use `android:lines` instead. same behaviour like `android:maxLines` – mochadwi May 08 '19 at 07:23
  • 1
    @mochadwi android:lines doesn't work with ellipsizing for me. – Kiryl Tkach Aug 02 '21 at 13:46
  • singleLine set to true causes maxLength to be ignored. – Ezequiel Adrian Dec 30 '21 at 21:23
  • The one difference I saw between `android:lines` and `singleLine` is when the content is longer than screen size, in singleLine the content ends with `...` . – Rishabh Patel Apr 25 '22 at 06:45
0

maxLines

In EditText, maxLines property set with the particular value like 5 then only five line text is visible in EditText as well as the Enter key in
keypad also visible for Entering.

singleLine

In EditText, singleLine property is set with true value then only one line
text is int EditText visible as well as the Enter key in keypad not visible for Entering to us.

Thanks

Solace
  • 8,612
  • 22
  • 95
  • 183
Mahesh Suthar
  • 237
  • 1
  • 11