1

We can set single line with this

TextView textView = (TextView) view.findViewById(R.id.someTextView);
textView.setSingleLine(true);

But for get status of single line event getSingleLine() does not exit.

How can check that single line for text view is on (true) or off (false)?

My target API level is 15.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82

2 Answers2

3

I believe singleLine is deprecated, you should use textView.setMaxLines(1) instead and then use textView.getMaxLines() so to know if textViewis singleLine :

boolean isSingleLine = textView.getMaxLines() == 1;

EDIT FOR API 15

But in case you are using singleLine in that case to know if the TextView is singleLine you can use getTransformationMethod() available from API 1 onwards like so :

boolean isSingleLine = textView.getTransformationMethod() == SingleLineTransformationMethod.getInstance();
Amr El Aswar
  • 3,395
  • 3
  • 23
  • 36
  • 2
    return textView.getMaxLines() == 1; would suffice – Willi Mentzel Dec 21 '16 at 15:44
  • @WilliMentzel yep you're right, but I prefer writing it like this for clarity of explanation but thanks for the suggestion – Amr El Aswar Dec 21 '16 at 15:45
  • why the downvote ? Perfectly valid answer, user added API 15 only after the answer had been posted – Amr El Aswar Dec 21 '16 at 15:55
  • no downvote: but why did you edit your answer, after explaining to me why you don't write it the way I proposed :D? plus the return is totally out of context.. you should wrap it in a method – Willi Mentzel Dec 21 '16 at 15:57
  • 1
    @WilliMentzel I said you were right and I edited it based on your suggestion I just have the bad habit of putting 'if' everywhere when explaining :p – Amr El Aswar Dec 21 '16 at 16:00
  • wrap it in a method a la isSingleLine or remove the return that would really improve your answer! i would go for option one – Willi Mentzel Dec 21 '16 at 16:02
1

You should use getMaxLines().

tv.setSingleLine( true ) || tv.setMaxLines( 1 );

int maxLines = tv.getMaxLines();

Assert.assertTrue( 1, maxLines );

The docs for setSingleLine() say that:

Sets the properties of this field (lines, horizontally scrolling, transformation method) to be for a single-line input.

My undesrtanding of this, is that they are juts putting a nice wrapper around setMaxLines(1) and some others.

And under the covers, this method does indeed set the line count to 1.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123