Dpedrinha
Dpedrinha

Reputation: 4220

How to make Xamarin.Forms.Editor scrollable/auto resizable?

I have an scrollabe layout with an Editor inside that I'd like to make scrollable or auto resizable to fit the contents.

I can't find how to do it.

I tried a custom renderer but I can't find how to set InputMethods to the Control.

Any ideas?

Upvotes: 4

Views: 4860

Answers (1)

Dpedrinha
Dpedrinha

Reputation: 4220

With the help of this post: https://forums.xamarin.com/discussion/80360/editor-inside-scrollview-not-scrolling

That fixed the scrolling on Android (iOS works by default). It avoids the parent scroll event when touching inside the Editor, triggering only the Editor scroll.

First a class on Android project:

using Android.Views;
namespace MyApp.Droid
{
    public class DroidTouchListener : Java.Lang.Object, View.IOnTouchListener
    {
        public bool OnTouch(View v, MotionEvent e)
        {
            v.Parent?.RequestDisallowInterceptTouchEvent(true);
            if ((e.Action & MotionEventActions.Up) != 0 && (e.ActionMasked & MotionEventActions.Up) != 0)
            {
                v.Parent?.RequestDisallowInterceptTouchEvent(false);
            }
            return false;
        }
    }
}

And then use it on the Android Custom EditorRenderer:

protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
    base.OnElementChanged(e);
    if (e.OldElement == null)
    {
        var nativeEditText = (global::Android.Widget.EditText)Control;

        //While scrolling inside Editor stop scrolling parent view.
        nativeEditText.OverScrollMode = OverScrollMode.Always;
        nativeEditText.ScrollBarStyle = ScrollbarStyles.InsideInset;
        nativeEditText.SetOnTouchListener(new DroidTouchListener());

        //For Scrolling in Editor innner area
        Control.VerticalScrollBarEnabled = true;
        Control.MovementMethod = ScrollingMovementMethod.Instance;
        Control.ScrollBarStyle = Android.Views.ScrollbarStyles.InsideInset;
        //Force scrollbars to be displayed
        Android.Content.Res.TypedArray a = Control.Context.Theme.ObtainStyledAttributes(new int[0]);
        InitializeScrollbars(a);
        a.Recycle();
    }
}

Upvotes: 7

Related Questions