Android Question Cursor Control [SOLVED]

Roger Daley

Well-Known Member
Licensed User
Longtime User

klaus

Expert
Licensed User
Longtime User
You can do it with the current methods of an EditText view.

The code:
B4X:
Sub Globals
    Private btnLeft As Button
    Private btnRight As Button
    Private edtTest As EditText
    Private CursorPosition As Int    'variable keeping the current cursor position
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
End Sub

Sub btnLeft_Click
    Private i As Int
    edtTest.RequestFocus               'sets the focus to show the cursor
    i = edtTest.SelectionStart         'gets the current cursor position
    CursorPosition = Max(i - 1, 0)    'removes one till 0
    edtTest.SelectionStart = CursorPosition    'sets the new position
End Sub

Sub btnRight_Click
    Private i As Int
    edtTest.RequestFocus                  'sets the focus to show the cursor
    i = edtTest.SelectionStart            'gets the current cursor position
    CursorPosition = Min(i + 1, edtTest.Text.Length)    'adds one till text length
    edtTest.SelectionStart = CursorPosition    'sets the new position
End Sub

It could also be done in one routine using the Sender object, I found it easier readable with two routines.

Attached a small projet.

I have not found how to change the cursor color.
 

Attachments

  • EditTextCursor.zip
    7.9 KB · Views: 307
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Klaus,

As always your solution is clear and elegantly simple.
I have however a problem, cursor is not showing. This is an old project getting an update and I dimly remember hiding the cursor but now can't remeber how despite scrolling through my code.

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Klaus,

As always your solution is clear and elegantly simple.
I have however a problem, cursor is not showing. This is an old project getting an update and I dimly remember hiding the cursor but now can't remeber how despite scrolling through my code.

Regards Roger


Problem solved. I had left the Enabled box in the designer Proerties unticked. Tick box for cursor or Enable/Disable in code.
EG edtTest.Enabled = True
 
Upvote 0
Top