Is there a way to detect if the cursor is on a new line in an EditText? I have a button that inserts numbers but it can only do it if the cursor is on a new line.
e.g.
- If the user has not typed anything at all and the button is pressed, a number will be inserted
- If the user is in the middle of a line and the button is pressed, it will automatically go to the next line and then enter a number.
- If the user has any amount of lines above the cursor, but the cursor is at the start of a new line and the button is pressed, a number will be inserted.
I can’t test if .getSelectionStart() == 0 on my EditText, because that will not help for the second or third case.
Thank you!
EDIT: I know I asked a similar question already, but the answer I got from that did not help, so I decided to ask a more specific and thus completely different question.
Search through the string for “\n”. That should be the new line character.
If the last character is \n, you’re on a new line.
Hope this helps!