Android Singletap/onclick In Webview
Solution 1:
WebView
does not support the OnClickListener
. And also it does consume touch events even though nothing happened on the web page thus ancestors views (like your LinearLayout
) don't have any chance to produce an OnClick event. It is very unfortunate.
As a workaround I extended a RelativeLayout
and put my WebView
inside of it. In the RelativeLayout
I've overwritten onInterceptTouchEvent
and were looking for tap events. If a tap is detected then the RelativeLayout
's OnClickListener
is invoked with performClick()
.
publicclassTapAwareRelativeLayoutextendsRelativeLayout {
privatefinalfloatMOVE_THRESHOLD_DP=20 * getResources().getDisplayMetrics().density;
privateboolean mMoveOccured;
privatefloat mDownPosX;
privatefloat mDownPosY;
publicTapAwareRelativeLayout(Context context) {
super(context);
}
publicTapAwareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
finalintaction= 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;
}
returnsuper.onInterceptTouchEvent(ev);
}
}
Solution 2:
I modified Zsolt Safrany's answer and I put his onInterceptTouchEvent
's content into webview's onTouch
method and it worked.
webview.setOnTouchListener(newView.OnTouchListener() {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
Log.v(TAG,"Got a touch event in the web view!");
final int action = ev.getAction();
switch (action) {
caseMotionEvent.ACTION_DOWN:
mMoveOccured = false;
mDownPosX = ev.getX();
mDownPosY = ev.getY();
break;
caseMotionEvent.ACTION_UP:
if (!mMoveOccured) {
//click operation is here
}
break;
caseMotionEvent.ACTION_MOVE:
if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
mMoveOccured = true;
}
break;
}
returnfalse;
} });
Solution 3:
From this page: http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
It seems that touch events must be properly trapped. Check this out:
@OverridepublicbooleandispatchTouchEvent(MotionEvent e){
super.dispatchTouchEvent(e);
return mGestureDetector.onTouchEvent(e);
}
Solution 4:
Worked code ::
finalBoolean[] mMoveOccured = newBoolean[1];
finalfloat[] mDownPosX = newfloat[1];
finalfloat[] mDownPosY = newfloat[1];
finalfloat MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;
userPic.setOnTouchListener(new View.OnTouchListener() {
@Override
publicboolean onTouch(View v, MotionEvent event) {
finalint 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;
}
returnfalse;
}
});
Post a Comment for "Android Singletap/onclick In Webview"