The TextView(left) shows the number of lines(using the getLineCount method) in the EditText(right). However the getLineCount method returns a value of 2 even though there's only 1 line. Initially it returns 1 but it returns 2 as soon as the viewport is filled with characters and on addition of another character. Please note that the EditText is inside a HorizontalScrollView, so that the text doesn't wrap to the next line. I have added pictures to explain this effectively. https://i.stack.imgur.com/BNABs.png https://i.stack.imgur.com/WJ2BW.png
Asked
Active
Viewed 852 times
1 Answers
1
getLineCount()
internally uses StaticLayout or DynamicLayout to measure content and it doesn't reflect EditText one to one. I'm not sure about the measurement details, but apparently you're interested in line count by line endings. If so, you can just count line endings in text and add 1. See: Count the number of lines in a Java String

Zielony
- 16,239
- 6
- 34
- 39
-
But doesn't that require quite a lot memory if the number of lines are, say, like 1000 or more? – Asif Nassar Dec 11 '18 at 14:59
-
Not really. You still need to keep that text in memory and measure it somehow. Looking for a specific character in text is a much lighter operation than measuring using DynamicLayout. Just make sure that you don't use String.split() because that would create a String for each line. A simple loop would be fine. – Zielony Dec 11 '18 at 15:04