Android Question Select and highlight next word in EditText

Hedi

Member
Hello,

How can I select word by word in EditText?

For Example:

I have this sentence: How can I select next word

First select and highlight (How) then second (can) and so on.

Doing this all by a button.

Thanks in advance.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1682833397741.png


B4X:
Private Sub Button1_Click
    Dim CurrentSelectionEnd As Int = EditText1.SelectionStart + EditText1.SelectionLength
    If SelectNextWord(CurrentSelectionEnd) = False Then
        SelectNextWord(0)
    End If
End Sub

Private Sub SelectNextWord(StartOffset As Int) As Boolean
    Dim m As Matcher = Regex.Matcher("(\b[^\s]+\b)", EditText1.Text)
    Do While m.Find
        If m.GetStart(0) >= StartOffset Then
            EditText1.SetSelection(m.GetStart(0), m.GetEnd(0) - m.GetStart(0))
            EditText1.RequestFocus
            Return True
        End If
    Loop
    Return False
End Sub
 
Upvote 0
Top