B4J Question Programmatically Inserting Text Into TextArea

B4XDev

Member
Licensed User
I have a ListView of items that can be clicked on to be inserted into a text document in a TextArea view.

Here's how I'm doing it...

B4X:
Private Sub list_Fields_MouseClicked (EventData As MouseEvent)
    Private iText As String = "{{" & list_Fields.SelectedItem & "}}"
    Private ip As Int = txt_EmailBody.SelectionStart
    Private oldText, newText As String
    list_Fields.SelectedIndex = -1
    oldText = txt_EmailBody.Text
    newText = oldText.SubString2(0,ip) & iText & oldText.SubString(ip)
    txt_EmailBody.Text = newText
    txt_EmailBody.SelectionStart = ip + iText.Length
    txt_EmailBody.RequestFocus
End Sub

Is there a better way? Does a TextArea have an InsertAt() type function? I couldn't find one, but that doesn't mean it doesn't exist!
 

stevel05

Expert
Licensed User
Longtime User
It's parent object TextAreaControl does have an insertText method which you can access via JavaObject.

B4X:
TextArea1.As(JavaObject).RunMethod("insertText",Array(TextArea1.SelectionStart,"New Text"))

But it only really does what you have implemented, so whether it's worth using it is debatable.

See the javadoc here.
 
Upvote 0
Top