title: How to properly implement NestedScrollingChild on a WebView
body: <p>The <code>NestedScrollView</code> provided with the support library doesn't work properly with the <code>WebView</code> (related <a href="https://code.google.com/p/android/issues/detail?id=198965" rel="noreferrer">bug report</a>). Because of this I have set out to implement <code>NestedScrollingChild</code> for a <code>WebView</code> and it appears to work fine for the first scroll, collapsing and expanding the <code>AppBarLayout</code> but any subsequent scrolls fail to collapse it. </p>
  
  <p>My <code>WebView</code>:</p>
  
  <pre><code>import android.annotation.TargetApi;
  import android.content.Context;
  import android.os.Build;
  import android.support.v4.view.NestedScrollingChild;
  import android.support.v4.view.NestedScrollingChildHelper;
  import android.util.AttributeSet;
  import android.view.View;
  import android.webkit.WebView;
  
  /**
   * Created by carlos on 2/19/16.
   */
  public class MyWebView extends WebView implements NestedScrollingChild {
      private static final String TAG = MyWebView.class.getSimpleName();
      private NestedScrollingChildHelper helper = new NestedScrollingChildHelper(this);
  
      public MyWebView(Context context) {
          super(context);
      }
  
      public MyWebView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
      }
  
      @TargetApi(Build.VERSION_CODES.LOLLIPOP)
      public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
      }
  
      public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
          super(context, attrs, defStyleAttr, privateBrowsing);
      }
  
      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  
          int dx = l - oldl;
          int dy = t - oldt;
          if (dy != 0) helper.startNestedScroll(View.SCROLL_AXIS_VERTICAL);
          helper.dispatchNestedPreScroll(dx, dy, null, null);
          if (dy != 0) helper.stopNestedScroll();
  
          super.onScrollChanged(l, t, oldl, oldt);
      }
  
  
      @Override
      public boolean isNestedScrollingEnabled() {
          return helper.isNestedScrollingEnabled();
      }
  
      @Override
      public void setNestedScrollingEnabled(boolean enabled) {
          helper.setNestedScrollingEnabled(enabled);
      }
  
      @Override
      public boolean startNestedScroll(int axes) {
          return helper.startNestedScroll(axes);
      }
  
      @Override
      public void stopNestedScroll() {
          helper.stopNestedScroll();
      }
  
      @Override
      public boolean hasNestedScrollingParent() {
          return helper.hasNestedScrollingParent();
      }
  
      @Override
      public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
          return helper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
      }
  
      @Override
      public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
          return helper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
      }
  
      @Override
      public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
          return helper.dispatchNestedFling(velocityX, velocityY, consumed);
      }
  
      @Override
      public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
          return helper.dispatchNestedPreFling(velocityX, velocityY);
      }
  }
  </code></pre>
  
  <p>My activity:</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.WebSettings;
  import android.webkit.WebViewClient;
  
  public class ScrollingActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_scrolling);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
          MyWebView view = (MyWebView) findViewById(R.id.webview);
          view.setNestedScrollingEnabled(true);
          view.setWebViewClient(new WebViewClient());
          WebSettings settings = view.getSettings();
          settings.setJavaScriptEnabled(true);
          settings.setAllowContentAccess(true);
          settings.setAppCacheEnabled(true);
          settings.setDatabaseEnabled(true);
          settings.setDomStorageEnabled(true);
          settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
          settings.setJavaScriptEnabled(true);
          settings.setSupportZoom(true);
          settings.setJavaScriptCanOpenWindowsAutomatically(true);
          settings.setBuiltInZoomControls(true);
  
          settings.setAppCacheEnabled(true);
          settings.setAppCachePath(getCacheDir().getAbsolutePath());
          settings.setDatabaseEnabled(true);
          settings.setSupportMultipleWindows(true);
          settings.setLoadWithOverviewMode(true);
          settings.setUseWideViewPort(true);
          settings.setDomStorageEnabled(true);
          settings.setAllowContentAccess(true);
          settings.setAllowFileAccess(true);
          settings.setSaveFormData(true);
          // findViewById(R.id.nested).setLayerType(View.LAYER_TYPE_HARDWARE, null);
          // view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
          view.loadUrl("http://vimeo.com");
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_scrolling, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>My styles.xml</p>
  
  <pre><code>&lt;resources&gt;
  
      &lt;!-- Base application theme. --&gt;
      &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt;
          &lt;!-- Customize your theme here. --&gt;
          &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
          &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
          &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.NoActionBar"&gt;
          &lt;item name="windowActionBar"&gt;false&lt;/item&gt;
          &lt;item name="windowNoTitle"&gt;true&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/&gt;
  
      &lt;style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>My dimensions file:</p>
  
  <pre><code>&lt;resources&gt;
      &lt;dimen name="app_bar_height"&gt;180dp&lt;/dimen&gt;
      &lt;dimen name="fab_margin"&gt;16dp&lt;/dimen&gt;
      &lt;dimen name="text_margin"&gt;16dp&lt;/dimen&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>My activity layout (<code>activity_scrolling.xml</code>):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="com.childscrollview.ScrollingActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:id="@+id/app_bar"
          android:layout_width="match_parent"
          android:layout_height="@dimen/app_bar_height"
          android:fitsSystemWindows="true"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/toolbar_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fitsSystemWindows="true"
              app:contentScrim="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|snap|enterAlways"&gt;
  
              &lt;android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  app:layout_collapseMode="pin"
                  app:popupTheme="@style/AppTheme.PopupOverlay"/&gt;
  
          &lt;/android.support.design.widget.CollapsingToolbarLayout&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;com.childscrollview.MyWebView
          android:id="@+id/webview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          &gt;&lt;/com.childscrollview.MyWebView&gt;
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/fab_margin"
          android:src="@android:drawable/ic_dialog_email"
          app:layout_anchor="@id/app_bar"
          app:layout_anchorGravity="bottom|end"/&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>So what is wrong with my implementation or is this a bug? </p>
  
  <p>Thanks. </p>
  
  <p>EDIT: Google now says the <a href="https://code.google.com/p/android/issues/detail?id=201385" rel="noreferrer">WebView isn't supported</a> like this. </p>