Android left/right swipe gesture in WebView activity (with clicking on links and vertical srolling)












5















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 throught 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://stackoverflow.com/a/20005481/3272449, 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 srolling and clicking on links working at the same time?



SOLUTION: Same solved question:
Fling Gesture and Webview in Android










share|improve this question





























    5















    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 throught 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://stackoverflow.com/a/20005481/3272449, 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 srolling and clicking on links working at the same time?



    SOLUTION: Same solved question:
    Fling Gesture and Webview in Android










    share|improve this question



























      5












      5








      5








      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 throught 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://stackoverflow.com/a/20005481/3272449, 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 srolling and clicking on links working at the same time?



      SOLUTION: Same solved question:
      Fling Gesture and Webview in Android










      share|improve this question
















      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 throught 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://stackoverflow.com/a/20005481/3272449, 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 srolling and clicking on links working at the same time?



      SOLUTION: Same solved question:
      Fling Gesture and Webview in Android







      android webview swipe gestures






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:01









      Community

      11




      11










      asked Feb 4 '14 at 22:44









      James07James07

      148311




      148311
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'm not familiar enough with the GestureDetector to reliably diagnose your problem. One thing you could try is cloning the event:



          @Override
          public boolean dispatchTouchEvent(MotionEvent ev){
          onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
          return super.dispatchTouchEvent(ev);
          }


          Failing that another way to approach this is to use onOverScrolled instead of a GestureDetector:



          @Override
          protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
          if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
          onSwipeLeft();
          }
          if (scrollX < - THRESHOLD && clampedX) {
          onSwipeRight();
          }
          super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
          }


          This does assume that your content is not normally scrollable sideways though.






          share|improve this answer
























          • Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

            – James07
            Feb 5 '14 at 19:50











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21565131%2fandroid-left-right-swipe-gesture-in-webview-activity-with-clicking-on-links-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I'm not familiar enough with the GestureDetector to reliably diagnose your problem. One thing you could try is cloning the event:



          @Override
          public boolean dispatchTouchEvent(MotionEvent ev){
          onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
          return super.dispatchTouchEvent(ev);
          }


          Failing that another way to approach this is to use onOverScrolled instead of a GestureDetector:



          @Override
          protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
          if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
          onSwipeLeft();
          }
          if (scrollX < - THRESHOLD && clampedX) {
          onSwipeRight();
          }
          super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
          }


          This does assume that your content is not normally scrollable sideways though.






          share|improve this answer
























          • Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

            – James07
            Feb 5 '14 at 19:50
















          0














          I'm not familiar enough with the GestureDetector to reliably diagnose your problem. One thing you could try is cloning the event:



          @Override
          public boolean dispatchTouchEvent(MotionEvent ev){
          onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
          return super.dispatchTouchEvent(ev);
          }


          Failing that another way to approach this is to use onOverScrolled instead of a GestureDetector:



          @Override
          protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
          if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
          onSwipeLeft();
          }
          if (scrollX < - THRESHOLD && clampedX) {
          onSwipeRight();
          }
          super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
          }


          This does assume that your content is not normally scrollable sideways though.






          share|improve this answer
























          • Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

            – James07
            Feb 5 '14 at 19:50














          0












          0








          0







          I'm not familiar enough with the GestureDetector to reliably diagnose your problem. One thing you could try is cloning the event:



          @Override
          public boolean dispatchTouchEvent(MotionEvent ev){
          onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
          return super.dispatchTouchEvent(ev);
          }


          Failing that another way to approach this is to use onOverScrolled instead of a GestureDetector:



          @Override
          protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
          if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
          onSwipeLeft();
          }
          if (scrollX < - THRESHOLD && clampedX) {
          onSwipeRight();
          }
          super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
          }


          This does assume that your content is not normally scrollable sideways though.






          share|improve this answer













          I'm not familiar enough with the GestureDetector to reliably diagnose your problem. One thing you could try is cloning the event:



          @Override
          public boolean dispatchTouchEvent(MotionEvent ev){
          onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev));
          return super.dispatchTouchEvent(ev);
          }


          Failing that another way to approach this is to use onOverScrolled instead of a GestureDetector:



          @Override
          protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
          if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
          onSwipeLeft();
          }
          if (scrollX < - THRESHOLD && clampedX) {
          onSwipeRight();
          }
          super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
          }


          This does assume that your content is not normally scrollable sideways though.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 5 '14 at 18:21









          marcin.kosibamarcin.kosiba

          2,791815




          2,791815













          • Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

            – James07
            Feb 5 '14 at 19:50



















          • Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

            – James07
            Feb 5 '14 at 19:50

















          Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

          – James07
          Feb 5 '14 at 19:50





          Adding MotionEvent.obtain(ev) doesn't change anything, there is still no vertical scrolling and debug output is same as previous. Overriding onOverScrolled doesn't work, because this expect scrollable sideways. If I don't scroll to the side, then scrollX is still zero and onSwipeLeft/Right won't occur.

          – James07
          Feb 5 '14 at 19:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21565131%2fandroid-left-right-swipe-gesture-in-webview-activity-with-clicking-on-links-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre