Search code examples
formsxamarinxamarin.formscursor-positionxamarin.forms.entry

How do I change the cursor position of an entry manually in xamarin forms?


I'm having trouble changing the position of an Entry cursor. Is there a way for me to somehow change the position manually?

I have a hidden Entry which is bound to a Label that displays TotalAmount (a number). The Label is inside a StackLayout which has a TapGestureRecognizer="OnTapped". The OnTapped function sets the focus on the hidden Entry so that the user can input a number. But there is also button in the same view that the user can click and a fixed number is added to TotalAmount. Now when the user taps the StackLayout again (after having used the 'fixed number' button), and the focus is set, the cursor position is at the front, not at the end like I want it to. This only happens on Android, not on IOS or Windows.

I have tried using a custom render and override OnElementPropertyChanged but I cant find a way to change it there. I also tried overriding OnFocusChanged but it never gets called.

All tips, examples or advice are welcome.


Solution

  • On Android, use the SetSelection method to set the cursor position

    // both arguments should match to just set the cursor position
    textview.SetSelection(1,1);