1

New-ish to Android and Xamarin, working in VS 2019, Xamarin Forms v4.8 ...

In my project, I have implemented a custom renderer for an Editor that provides a read-only Editor in which the user can select and copy text. Works great, with customized context menu (removing Cut and Paste options). Thank you Anna Domashych! (https://medium.com/@anna.domashych/selectable-read-only-multiline-text-field-in-xamarin-forms-69d09276d580)

I also want to provide a search function, where user enters text to be found (in an Entry or SearchBar), and the app finds and selects the found text, and then scrolls the found text into view, if necessary.

But I see no way to have the app select text in the Editor. An Entry has CursorPosition and SelectionLength properties, which I presume can be used to programmatically select text, but an Editor does not seem to have these properties. Call me crazy, but that fact seems particularly odd to me!

I was hoping I could extend my custom renderer, but custom renderers seem to provide a mechanism to set behavior-type properties that are not exposed in the cross platform Xamarin view (such as, SetTextIsSelectable). That is, it seems that a custom renderer cannot provide a means to set properties on the underlying Android control, from code in the shared project (dynamically?). In particular I am interested in the setSelection(int start, int stop) method of the Android EditText view.

Is there any way to do this?

Thanks

Blaise
  • 21
  • 7
  • https://forums.xamarin.com/discussion/73176/how-to-get-selected-text – Cfun Sep 13 '20 at 21:20
  • @Cfun Thanks for the response, but the subject discussed in the post that you have linked to is about an **Entry** view. An Entry is for a single line of text, and it has _CursorPosition_ and _SelectionLength_ properties. I am trying to work with the **Editor** view, which handles multi-line text and does not have properties similar to the Entry – Blaise Sep 14 '20 at 17:34
  • To set the cursor position of `Editor` in Xamarin Forms, try using the `Control.SetSelection` command in the custom renderer class. Check the link:https://stackoverflow.com/questions/62623756/set-cursorposition-in-editor-xamarin-forms – Jarvan Zhang Sep 16 '20 at 07:29
  • @JarvanZhang-MSFT TY for the response (sorry for my delayed reply, I didn't see notification of your comment). I thought I'd do something in the custom renderer, but I don't see how to do it _on demand_. The post at the link you provided sets the Control.SelectionStart property in the OnElementPropertyChanged event. I need to be able to set selection start and length in real time, during user interaction. Tried doing this with dependency service; it works, but it's gawd-awful ugly. Also when I set a selection, the Editor opens the keyboard ... another problem (my editor is read-only) – Blaise Oct 17 '20 at 23:37

0 Answers0