Xamarin Forms Shell: Handling Android Scrolling in WebViews

2019-07-26 13_14_18-android webview xamarin - Google Search.pngXamarin Forms Shell is an awesome new development tool allowing you to build amazing apps faster. Shell takes care of a lot of the plumbing so you can focus more on the app to make your customer happy, rather than the behind the scenes details needed to support your features.

There is a special case you do need to take care of on Android to allow the app to scroll WebViews as there currently is a bug. When a user tries to scroll, it won’t.

Instead of using WebView in your XAML, change that to use a new custom one that simply inherits from WebView:

namespace App.Views.Common
{
    /// <summary>
    /// Needed for Android scroll issue
    /// </summary>
    public class MyWebView  : WebView
    {
    }
}

In your XAML reference it will look like this:

<common:MyWebView 
x:Name="WebView" 
HorizontalOptions="FillAndExpand" 
VerticalOptions="FillAndExpand">
</common:MyWebView>

Now we add a renderer to the Android project:

[assemblyExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace App.Droid.Renderers
{
    class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }
 
        protected override void OnElementChanged(ElementChangedEventArgs<WebViewe)
        {
            base.OnElementChanged(e);
        }
 
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }
    }
}

 iOS requires no changes.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s