Selectable read-only multiline text field in Xamarin.Forms

While working on the cross-platform mobile application for Android and iOS I faced a business requirement to let user copy a part of data from a long text field without letting him modifying the content.

Here is what I was to achieve

And here is what I had using an Editor

I have posted related articles on how to achieve this behavior on native Android and iOS. Here you will find the full code for the cross-platform solution.

First, let’s create a new control in shared project inheriting from Editor:

iOS renderer is pretty straight-forward:

Android renderer leverages CustomSelectionActionModeCallback and CustomInsertionActionModeCallback properties of EditText to customize context menu for text selection and insertion:

If you, like me, face a known issue that text selection stops working when there is more than one line of text, the solution is to inherit the FormsEditText and reset Enabled property in OnAttachedToWindow callback (you might want to write more sophisticated solution if you need full support of Enabled property).

To use this control in renderer, override the CreateNativeControl method.

Creating complex cross-platform UI elements is as simple as the native implementation. If you want to go deeper into details, check out related articles with more comments on this solution for native Android and iOS.

--

--

Anna Leushchenko πŸ‘©β€πŸ’»πŸ’™πŸ“±πŸ‡ΊπŸ‡¦

Google Developer Expert in Dart and Flutter | Author, speaker at tech events, mentor, OSS contributor | Passionate mobile apps creator