Android Question Select a line on a multiline EditText when user tap on it

Mattiaf

Active Member
Licensed User
Hi, I'm looking for a way to select a line into a multiline EditText when the user tap on it..
Online i can only find ways to select the text between two indexes...
Thanks
 

klaus

Expert
Licensed User
Longtime User
I suppose that this is what you are looking for.

1668951917675.png


The code:

B4X:
Sub Globals
    Private edtTest As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    'adds a Click listener
    'as long as the EditText has the focus, the FocusChanged event is not raised, therefore the Click event
    Private refEditText As Reflector
    refEditText.Target = edtTest
    refEditText.SetOnClickListener("edtTest_Click")

    edtTest.Text = "This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text."
End Sub

'used for the first click, sets the focus and selects the cursor position
Private Sub edtTest_FocusChanged (HasFocus As Boolean)
    SelectLine
End Sub

'used for subsequent clicks when the EditText has the focus
Private Sub edtTest_Click(View As Object)
    SelectLine
End Sub

Private Sub SelectLine
    'SelectionStart = text cursor position
    'LineIndex = index of the line where the cursor is positioned
    'LineStart = text cursor position of the first character in the line
    'LineVisibleEnd = text cursor position of the last visible character in the line
    Private SelectionStart, LineIndex, LineStart, LineVisibleEnd As Int
    Private joEditText, joEditTextLayout As JavaObject
    
    joEditText = edtTest
    joEditTextLayout = joEditText.RunMethod("getLayout", Null)
    
    SelectionStart = joEditText.RunMethod("getSelectionStart", Null)
    LineIndex = joEditTextLayout.RunMethod("getLineForOffset", Array As Object(SelectionStart))
    LineStart = joEditTextLayout.RunMethod("getLineStart", Array As Object(LineIndex))
    LineVisibleEnd = joEditTextLayout.RunMethod("getLineVisibleEnd", Array As Object(LineIndex))
    joEditText.RunMethod("setSelection", Array As Object(LineStart, LineVisibleEnd))
End Sub
 

Attachments

  • EditTextDemo.zip
    9.4 KB · Views: 70
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I suppose that this is what you are looking for.
Great code, Klaus.

I haven't tried it, but there might be a problem: would the user be able to edit the text in the EditText?

I say this in general, since @Mattiaf doesn't want to allow it; he has just to show a list of data from which the user has to choose one value (so I modified his project, using a more suitable CustomListView).

Yours it's anyway good code, to which I would add the copy directly to the clipboard (and I would post it in the Snippets forum šŸ˜Š)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Upvote 0
Top