0

I an developing an app which shows the contents from database into webview.

In first screen i have index where user select an item and it opens the content in the webview. currently i have two buttons to go to next or previous item.

Now i want to replace buttons with swipe left/right to go to next or previous item.

Below is my code for webview

private void setupwebview(){
    final WebView wvBrowser = ( WebView ) findViewById( R.id.wvBrowser );
    wvBrowser.getSettings( ).setLoadsImagesAutomatically( true );
    wvBrowser.getSettings( ).setJavaScriptEnabled( true );
    wvBrowser.getSettings( ).setBuiltInZoomControls( true );

wvBrowser.loadDataWithBaseURL( sBanner, html, this.mimeType,
            this.encoding, null );

    wvBrowser.setWebViewClient( new DefinitionWebViewClient( ) );
    // wvBrowser.getZoomControls( ).setVisibility( View.VISIBLE );

    wvBrowser.getSettings( ).setBuiltInZoomControls( true );

    new Handler( ).postDelayed( new Runnable( )
    {

        public void run( )
        {
            wvBrowser.setVisibility( View.VISIBLE );

            //wvBrowser.reload( );
        }
    }, 100 );
}

above method i call in oncreate of the activity class.

Customewebview ::

public class CustomWebView extends WebView{
private GestureDetector gestureDetector;

public CustomWebView(Context context) {
    super(context);

}

public CustomWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}

public void setGestureDetector(GestureDetector gestureDetector) {
    this.gestureDetector = gestureDetector;
}



}

Gesture detector

private class CustomeGestureDetector extends SimpleOnGestureListener {      
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if(e1 == null || e2 == null) return false;
        if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
        else {
            try { // right to left swipe .. go to next page
                if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) {
                    Log.d( "gesture", "moving right to left" );
                    moveDown();
                    return true;
                } //left to right swipe .. go to prev page
                else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
                    Log.d( "gesture", "moving left to right" );
                    moveUp();

                    return true;
                } 
            } catch (Exception e) { // nothing
            }
            return false;
        }
    }      
}
Anjali
  • 1,623
  • 5
  • 30
  • 50
  • 1
    Check out [GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html). You can use it with WebView – Tushar Gogna Feb 12 '15 at 06:45
  • Swipe doesnt work on android 2.3 and below versions...anyone can please help... – Anjali Feb 23 '15 at 05:29
  • Is that same code working for above versions? – Tushar Gogna Feb 23 '15 at 05:36
  • Yes..it works fine in 3 and above.. – Anjali Feb 23 '15 at 05:45
  • Can I see the part of code where you have implemented Swipe? + Have you tried using GestureDetector? – Tushar Gogna Feb 23 '15 at 05:47
  • @Tushar did you find anything in the code? – Anjali Feb 23 '15 at 06:53
  • Try disabling zoom functionality of the WebView, sometimes child having listeners contradicts with the listeners of the parent, but it works for other devices. This code looks fine otherwise, might be a compatibility issue with lower API levels. Let me check if there is an alternative. Otherwise you will have to drop Gingerbread and lower models (which are not much used now anyways) – Tushar Gogna Feb 23 '15 at 07:10
  • ok..if swipe doesnt work then can i put transparent arrows to go to prev or next item in the webview. if yes can you please tell me how to do that? – Anjali Feb 23 '15 at 07:17
  • 1
    Yes, you can do that as well. Add a check for API level.. if it is less than 2.3 then show these buttons. And to see where to add them, post your XML! – Tushar Gogna Feb 23 '15 at 07:18

0 Answers0