Android Code Snippet EditText Utils

Discussion in 'Code Snippets' started by klaus, Aug 2, 2014.

  1. klaus

    klaus Expert Licensed User

    Subject: Routines for selecting text and cursor management.

    The result of these routines is visible only if the EditText has the focus !

    Reminder of existing methods:

    EditText.RequestFocus : Tries to set the focus to this view. Returns True if the focus was set.
    EditText.SelectAll : Selects the entire text of EditText
    EditText.SelectionStart : Gets or sets the selection start position (or the cursor position).
    Returns -1 if there is no selection or cursor.

    Sub: setCursorVisible
    Description: Sets the cursor visible or hides it.
    'Sets the cursor visible or hides it
    Sub setCursorVisible(edt As EditText, Visible As Boolean)
    Dim jo = edt As JavaObject
    "setCursorVisible"Array As Object(Visible))
    End Sub
    Sub: setTextIsSelectable
    Description: Sets the text selectable or not selectable.
    'Sets the text selectable or not selectable
    Sub setTextIsSelectable(edt As EditText, Selectable As Boolean)
    Dim jo = edt As JavaObject
    "setTextIsSelectable"Array As Object(Selectable))
    End Sub
    Sub: setSelection
    Description: Selects the text between the two indexes.
    'Selects the text between the two indexes.
    Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
    Dim jo = edt As JavaObject
    "setSelection"Array As Object(StartIndex, EndIndex))
    End Sub
    Sub: getSelectionStart
    Description: Returns selection start index.
    'Gets the selection start index
    Sub getSelectionStart(edt As EditTextAs Int
    Dim jo = edt As JavaObject
    Return jo.RunMethod("getSelectionStart"Null)
    End Sub
    Sub: getSelectionEnd
    Description: Returns the selection end index.
    'Gets the selection end index
    Sub getSelectionEnd(edt As EditTextAs Int
    Dim jo = edt As JavaObject
    Return jo.RunMethod("getSelectionEnd"Null)
    End Sub
    Sub: CopyToClipboard
    Description: Copies the selected text in the given EditText view to the clipboard.
    Needs the Clipboard library.
    Dim clip As BClipboard can be moved from the routine to Globals.
    'Copies the selected text from the given EditText view to the Clipboard
    Sub CopyToClipboard(edt As EditText)
    Dim txt As String
    Dim i1, i2 As Int
    Dim clip As BClipboard
    Dim jo = edt As JavaObject
        i1 = jo.RunMethod(
        i2 = jo.RunMethod(
        txt = edt.Text.SubString2(i1, i2)
    End Sub

    The JavaObject routines can also be used without a sub like below or like in the CopyToClipboard routine above:
    Dim jo = edt As JavaObject

    "setTextIsSelectable"Array As Object(True))
    "setCursorVisible"Array As Object(True))
    "setSelection"Array As Object(510))
    Dependencies: JavaObject library, Clipboard library (only for CopyToClipboard)

    Tags: EditText, selection, setSelection, text, setTextIsSelectable, cursor, setCursorVisible, copy, clipboard

    Attached a small test program.

    EDIT: 2014.08.04
    Added the getSelectionStart, getSelectionEnd and CopyToClipboard routines.

    EDIT: 2016.06.27
    Amended two errors. The methods getSelectionStart and getSelectionEnd didn't return a value.

    Attached Files:

    Last edited: Mar 10, 2017
  2. JohnD

    JohnD Active Member Licensed User

    This is great! If it's not too much work could you show how to copy what you have selected to the clipboard? Thanks in advance, JD. :)
  3. klaus

    klaus Expert Licensed User

    Have a look at post #1.
    JohnD likes this.
  4. JohnD

    JohnD Active Member Licensed User

    Nice! Adding paste from the clipboard was trivial. I added btnPasteFromClipboard to the Main layout in your zip file and pasted it to edtTest2 (in the sub below) that was already on your layout.
    Sub btnPasteFromClipboard_Click
        edtTest2.Text = clip.getText
    End Sub
  5. klaus

    klaus Expert Licensed User

    I did paste the selected text with the finger onto edtTest2, that's the reason why I added edtTest2.
  6. JohnD

    JohnD Active Member Licensed User

    Missed that. Sorry.
  7. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Klaus, for these code snippets. Very useful.

    I have noticed that the cursor is always white no matter what the edt.textcolor is. If the edt.color is white, the cursor will not show.

    I assume there is no way to change the color of the cursor?

    Best regards.

  8. klaus

    klaus Expert Licensed User

    On my device the cursor color is gray.
    I don't know how to change the cursor color.
  9. tdocs2

    tdocs2 Well-Known Member Licensed User

    Hello, Klaus.
    I tested with 4 devices: 4.0 to 4.4 - only on the Samsung Note 10 is the cursor color gray or light blue - on the other 3, the cursor disappears on the white background. Easy to work around - no white background.
    Thanks again and best wishes.
  10. LucaMs

    LucaMs Expert Licensed User

    Hi Klaus,

    in some function the return statement is missing.
  11. klaus

    klaus Expert Licensed User

    Thank you Luca.
    Amended the snippets in the first post.
    The routines were OK in the zip file.
    LucaMs likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice