Android singleTap / OnClick in WebView

I load the web view using an Html String, I want to show the navigation buttons when the user clicks on the web view, I tried With the onTouch listener, the touch event goes up when scrolling and clicking, but I want to catch a single tap / clickEvent, Do not scroll the Event regarding ..., I implemented SetOnClickListener for WebView and LinearLayout, none of them work for me Any help regarding this

+8
android android-webview
May 6 '11 at 5:34
source share
4 answers

WebView does not support OnClickListener . It also consumes touch events, even if nothing happened on the webpage, so ancestor views (like your LinearLayout ) cannot create an OnClick event. This is very unfortunate.

As a workaround, I extended the RelativeLayout and placed its WebView inside it. In RelativeLayout I onInterceptTouchEvent and looked for tap events. If an answer is found, then RelativeLayout OnClickListener is called using performClick() .

 public class TapAwareRelativeLayout extends RelativeLayout { private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density; private boolean mMoveOccured; private float mDownPosX; private float mDownPosY; public TapAwareRelativeLayout(Context context) { super(context); } public TapAwareRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured = false; mDownPosX = ev.getX(); mDownPosY = ev.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured) { // TAP occured performClick(); } break; case MotionEvent.ACTION_MOVE: if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) { mMoveOccured = true; } break; } return super.onInterceptTouchEvent(ev); } 

}

+24
Jan 30 2018-12-12T00:
source share

From this page: http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

It seems that touch events should be correctly captured. Check this:

 @Override public boolean dispatchTouchEvent(MotionEvent e){ super.dispatchTouchEvent(e); return mGestureDetector.onTouchEvent(e); } 
+2
May 24 '11 at 16:11
source share

I changed Zsolt Safrany's answer and I put it onInterceptTouchEvent in the webview onTouch method and it worked.

 webview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.v(TAG,"Got a touch event in the web view!"); final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured = false; mDownPosX = ev.getX(); mDownPosY = ev.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured) { //click operation is here } break; case MotionEvent.ACTION_MOVE: if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) { mMoveOccured = true; } break; } return false; } }); 
+2
Feb 04 '14 at 20:37
source share

Processed Code ::

 final Boolean[] mMoveOccured = new Boolean[1]; final float[] mDownPosX = new float[1]; final float[] mDownPosY = new float[1]; final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density; userPic.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mMoveOccured[0] = false; mDownPosX[0] = event.getX(); mDownPosY[0] = event.getY(); break; case MotionEvent.ACTION_UP: if (!mMoveOccured[0]) { Toast.makeText(v.getContext(), "Webview pressed", Toast.LENGTH_SHORT).show(); } break; case MotionEvent.ACTION_MOVE: if (Math.abs(event.getX() - mDownPosX[0]) > MOVE_THRESHOLD_DP || Math.abs(event.getY() - mDownPosY[0]) > MOVE_THRESHOLD_DP) { mMoveOccured[0] = true; } break; } return false; } }); 
0
Dec 24 '15 at 6:40
source share



All Articles