Android Question Get Y postition of cursor in edittext

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I need this to position a custom keyboard.
This post suggest it can be done easily:
but it doesn't compile.

RBS
 

Sagenut

Expert
Licensed User
Longtime User
Do you mean the position of the cursor in a specific character of the text in the Edittext?
B4X:
Edittext.SelectionStart
should return that position.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Do you mean the position of the cursor in a specific character of the text in the Edittext?
B4X:
Edittext.SelectionStart
should return that position.

No, that won't be accurate enough, although it could be made to work.
I need the actual screen Y position of the cursor.

RBS
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
The y position is the text base line, the red cross.
The coordinate origin is the top left corner of the EditText.
The routine returns an Array of Int. xy(0) = X and xy(1) = Y

1591625174480.png


The cross is drawn onto Panel with same size as the EditText.

B4X:
'gets the x and y coordinates of the cursor in an EditText view
Private Sub GetEditTextXYCursor(edt As EditText) As Int()
    Private joEditText, joLayout As JavaObject
    Private PaddingLeft, PaddingTop, ScrollY, Pos, Line, LineBaseline As Int
    Private xy(2) As Int

    joEditText = edt
    PaddingLeft = joEditText.RunMethod("getPaddingLeft", Null)
    PaddingTop = joEditText.RunMethod("getPaddingTop", Null)
    ScrollY = joEditText.RunMethod("getScrollY", Null)
'    pos = edt.SelectionStart
    Pos = joEditText.RunMethod("getSelectionStart", Null)
    joLayout = joEditText.RunMethod("getLayout", Null)
    Line = joLayout.RunMethod("getLineForOffset", Array As Object(Pos))    'line numbsr
    LineBaseline = joLayout.RunMethod("getLineBaseline", Array As Object(Line))

    xy(0) = joLayout.RunMethod("getPrimaryHorizontal", Array As Object(Pos)) + PaddingLeft
    xy(1) = LineBaseline + PaddingTop - ScrollY    'base line
    Return xy
End Sub

Attached my test project, it contains more properties.
 

Attachments

  • EditTextYXCursor.zip
    10.1 KB · Views: 169
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Here you are:
The y position is the text base line, the red cross.
The coordinate origin is the top left corner of the EditText.
The routine returns an Array of Int. xy(0) = X and xy(1) = Y

View attachment 95403

The cross is drawn onto Panel with same size as the EditText.

B4X:
'gets the x and y coordinates of the cursor in an EditText view
Private Sub GetEditTextXYCursor(edt As EditText) As Int()
    Private joEditText, joLayout As JavaObject
    Private PaddingLeft, PaddingTop, ScrollY, Pos, Line, LineBaseline As Int
    Private xy(2) As Int

    joEditText = edt
    PaddingLeft = joEditText.RunMethod("getPaddingLeft", Null)
    PaddingTop = joEditText.RunMethod("getPaddingTop", Null)
    ScrollY = joEditText.RunMethod("getScrollY", Null)
'    pos = edt.SelectionStart
    Pos = joEditText.RunMethod("getSelectionStart", Null)
    joLayout = joEditText.RunMethod("getLayout", Null)
    Line = joLayout.RunMethod("getLineForOffset", Array As Object(Pos))    'line numbsr
    LineBaseline = joLayout.RunMethod("getLineBaseline", Array As Object(Line))

    xy(0) = joLayout.RunMethod("getPrimaryHorizontal", Array As Object(Pos)) + PaddingLeft
    xy(1) = LineBaseline + PaddingTop - ScrollY    'base line
    Return xy
End Sub

Attached my test project, it contains more properties.

Thanks, that works very nicely indeed.

RBS
 
Upvote 0
Top