Android Question How to determine if cursor is at end of text in an edittext?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
One would think that this is easy but I haven't worked it out yet, for example:

B4X:
Sub CursorAtEndOfText(vEDT As EditText) As Boolean
    
    Dim iSelStart As Int = vEDT.SelectionStart
    Dim bCursorAtEnd As Boolean
    
    Log("CursorAtEndOfText, iSelStart: " & iSelStart)

    vEDT.SelectionStart = iSelStart + 1
    
    'will be one more even if we were at the end of the text
    Log("CursorAtEndOfText, vEDT.SelectionStart: " & vEDT.SelectionStart)
    
    bCursorAtEnd = (vEDT.SelectionStart = iSelStart)
    
    vEDT.SelectionStart = iSelStart 'put it back where it was
    
    Return bCursorAtEnd
    
End Sub

RBS
 

jkhazraji

Active Member
Licensed User
Longtime User
One would think that this is easy but I haven't worked it out yet, for example:

B4X:
Sub CursorAtEndOfText(vEDT As EditText) As Boolean
   
    Dim iSelStart As Int = vEDT.SelectionStart
    Dim bCursorAtEnd As Boolean
   
    Log("CursorAtEndOfText, iSelStart: " & iSelStart)

    vEDT.SelectionStart = iSelStart + 1
   
    'will be one more even if we were at the end of the text
    Log("CursorAtEndOfText, vEDT.SelectionStart: " & vEDT.SelectionStart)
   
    bCursorAtEnd = (vEDT.SelectionStart = iSelStart)
   
    vEDT.SelectionStart = iSelStart 'put it back where it was
   
    Return bCursorAtEnd
   
End Sub

RBS
B4X:
Sub IsCursorAtEnd(et As EditText) As Boolean
    Dim cursorPosition As Int = et.SelectionStart ' Get current cursor position
    Dim textLength As Int = et.Text.Length ' Get length of the text
    
    Return cursorPosition = textLength ' Check if cursor is at the end
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
B4X:
Sub IsCursorAtEnd(et As EditText) As Boolean
    Dim cursorPosition As Int = et.SelectionStart ' Get current cursor position
    Dim textLength As Int = et.Text.Length ' Get length of the text
   
    Return cursorPosition = textLength ' Check if cursor is at the end
End Sub
That is first code I tried, but it doesn't work if the text contains high Unix characters.

RBS
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
That is first code I tried, but it doesn't work if the text contains high Unix characters.

RBS
This works for all characters high, or low: just use the text 'A high Unix text: Παλσταιν' to test
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Edt As EditText
    Private tmr As Timer
    Private lblinfo As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    tmr.Initialize("tmr",1000)
    tmr.Enabled=True
End Sub
Sub  tmr_Tick
    If IsCursorAtEnd(Edt) Then
        lblinfo.Text="At end"
       
    Else
        lblinfo.Text="Not at end"  
    End If
   
End Sub
Sub Activity_Resume

End Sub
Sub IsCursorAtEnd(et As EditText) As Boolean
    Dim jo As JavaObject = et
    Dim cursorPosition As Int = jo.RunMethod("getSelectionStart", Null) ' Get current cursor position
   
    ' Convert the EditText text into a JavaObject and get the actual character length
    Dim charSequence As JavaObject = jo.RunMethod("getText", Null)
    Dim textLength As Int = charSequence.RunMethod("length", Null) ' Get length considering high Unicode characters
   
    Return cursorPosition = textLength ' Compare cursor position with actual length
End Sub
Also test emoji: '🙃❤️✍️🌹'
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This works for all characters high, or low: just use the text 'A high Unix text: Παλσταιν' to test
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Edt As EditText
    Private tmr As Timer
    Private lblinfo As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    tmr.Initialize("tmr",1000)
    tmr.Enabled=True
End Sub
Sub  tmr_Tick
    If IsCursorAtEnd(Edt) Then
        lblinfo.Text="At end"
      
    Else
        lblinfo.Text="Not at end" 
    End If
  
End Sub
Sub Activity_Resume

End Sub
Sub IsCursorAtEnd(et As EditText) As Boolean
    Dim jo As JavaObject = et
    Dim cursorPosition As Int = jo.RunMethod("getSelectionStart", Null) ' Get current cursor position
  
    ' Convert the EditText text into a JavaObject and get the actual character length
    Dim charSequence As JavaObject = jo.RunMethod("getText", Null)
    Dim textLength As Int = charSequence.RunMethod("length", Null) ' Get length considering high Unicode characters
  
    Return cursorPosition = textLength ' Compare cursor position with actual length
End Sub
Also test emoji: '🙃❤️✍️🌹'
I was testing with a complex edittext with formatted SQL and although I put the cursor at the end of the text, for some reason there were still non-visible characters
so that messed matters up. The code I posted wouldn't work if the cursor really was at the end as there would be an error.

This simple code works fine:

B4X:
Sub CursorAtEndOfText(et As EditText) As Boolean
    
    Dim iSelStart As Int = et.SelectionStart
    
    Try
        et.SelectionStart = iSelStart + 1
        Return False
    Catch
        Return True
    End Try
    
End Sub

Just working out now what caused the non-visible characters at the end of my complex edittext, but that is entirely not related to the posted question.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
B4X:
Sub IsCursorAtEnd(et As EditText) As Boolean
    Dim cursorPosition As Int = et.SelectionStart ' Get current cursor position
    Dim textLength As Int = et.Text.Length ' Get length of the text
   
    Return cursorPosition = textLength ' Check if cursor is at the end
End Sub
All sorted now.
Of course the above simple code is fine and should have investigated better why that didn't work in the first place.
Sorry to have caused some confusion.

RBS
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
My intention of posting the code is that I thought somebody is looking for an answer.
Thanks.
 
Upvote 0
Top