Forum Xamarin.Android

How to get selected Text?

FosoFoso DEMember

Hi, i want to get the selected Text from an Entry. I'm using an EntryRenderer and the methods SelectionStart and SelectionEnd, but the returned value of the methods is identical.

Can you give me a short example how to get the selected Text? I don't know what i'm doing wrong.
Thank you

Posts

  • FosoFoso DEMember

    For anyone having the same problem, this was my solution:

    public class MyEditorRenderer : EditorRenderer
    {
    MyEditor myEditor;

        public MyEditorRenderer()
        {   }
    
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                myEditor = (MyEditor)base.Element;
    
                Control.LayoutChange += (sender, ev) => {
                    int start = Control.SelectionStart;
                    int end = Control.SelectionEnd;
                    int selectionLength = end - start;
    
                    if (end > 0 && end > start)
                    {
                        string selection = Control.Text.Substring(start, selectionLength);
                        myEditor.SelectionText = selection;
                    }
                };
    
            }
    
        }
    

    }

  • azrinsaniazrinsani Member ✭✭

    In android this selects the text but the copy,cut, paste context menu is not shown, which renders this feature useless.

Sign In or Register to comment.