A working solution to selecting text in entry on focus

DarrenKayDarrenKay GBUniversity ✭✭

Created a custom Entry renderer to allow me to select all text in an entry box when it gains focus.

  1. Control.SetSelectAllOnFocus(true) - Works in UI but not programmatically. Click off entry then click back selects all, but restricts entry to the size of selected text. Eg auto selects '12', you type '345' and it shows '45' as selected text?? Triggering focus in code does not trigger select all.
  2. Control.SelectAll() - Attached event handler for FocusChange, call SelectAll() and nothing is selected.
  3. Control.SetSelection() - As 2, in FocusChange, SetSelection(0, Control.Text.Length -1) and nothing is selected.

Is there a working way to select all text in a text entry in an Android?

Best Answer

  • DarrenKayDarrenKay GB ✭✭
    Accepted Answer

    Have a working solution using XF 3.2 which adds two new properties to Entry, CursorPosition and SelectionLength.

Answers

  • robbitrobbit CNMember Xamurai

    HI @DarrenKay ,

    Please read this.

    In your OnElementChanged :

        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);
            if (e.OldElement == null) {
                var nativeEditText = (global::Android.Widget.EditText)Control;
                nativeEditText.SetSelectAllOnFocus (true);
            }
        }
    
  • DarrenKayDarrenKay GBUniversity ✭✭

    Get a weird behaviour trying that. The entry has a value of '6' in it, I click on the entry to edit it and selects the value '6'. When I type it only allows one character? Eg. type 1234, it shows each digit in place of previous so I end up with just '4' in the entry box.

  • DarrenKayDarrenKay GBUniversity ✭✭
    Accepted Answer

    Have a working solution using XF 3.2 which adds two new properties to Entry, CursorPosition and SelectionLength.

Sign In or Register to comment.