Ziteng Chen solution works up to Android 4.0 (API level 15), but for some reason KeyEvent does not work in LEVEL 16+ API (Android 4.1+ JELLY_BEAN). It does not start loading WebView loadUrl. So I had to replace dispatchKeyEvent dispatchTouchEvent. Here is the code:
... MotionEvent touchDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, touchX, touchY, 0); webView.dispatchTouchEvent(touchDown); touchDown.recycle(); MotionEvent touchUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, touchX, touchY, 0); webView.dispatchTouchEvent(touchUp); touchUp.recycle(); String url = mUrl; ...
You probably have to wait (use AsyncTask) to get mUrl on slower devices, where it is right after running dispatchTouchEvents
Hope this helps.
source share