SOLVED, solution is underneath.
I want to replace buttons for navigation in webview to 2 swipe gestures - left and right swipe. This Android: How to handle right to left swipe gestures link really helped me, but I encountered problem - after implementing this code I was unable to click on links/ or scroll through loaded webpage in webview activity(I have used 1st answer from possible answers).
Then I found out that in another answer in this question is something similar - how to scroll in listview - https://mcmap.net/q/74031/-android-how-to-handle-right-to-left-swipe-gestures, but I wasn't able to get it running. In logcat i get this debug info: http://s27.postimg.org/5qfipgi1v/message.png
This is my OnSwipeTouchListener:
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public GestureDetector getGestureDetector(){
return gestureDetector;
}
}
And my webview activity(stripped):
public class WebViewActivity extends Activity {
private WebView browser;
private OnSwipeTouchListener onSwipeTouchListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
onSwipeTouchListener = new OnSwipeTouchListener() {
public void onSwipeRight() {
Toast.makeText(WebViewActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(WebViewActivity.this, "left", Toast.LENGTH_SHORT).show();
}
};
browser.setOnTouchListener(onSwipeTouchListener);
} // end of onCreate
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
onSwipeTouchListener.getGestureDetector().onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
} // end of WebViewActivity
How to make left/right swiping, vertical scrolling and clicking on links working at the same time?
SOLUTION: Same solved question: Fling Gesture and Webview in Android