How to disable horizontal scrolling in Android webview

android, java, webview

Solution

Here is how I disable horizontal scrolling only for a webview.

webView.setHorizontalScrollBarEnabled(false);
webView.setOnTouchListener(new View.OnTouchListener() {
    float m_downX;
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getPointerCount() > 1) {
            //Multi touch detected
            return true;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                // save the x
                m_downX = event.getX();
                break;
            }
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP: {
                // set x so that it doesn't move
                event.setLocation(m_downX, event.getY());
                break;
            }

        }
        return false;
    }
});

Basically intercept the touch event and don't allow the x value to change. This allows the webview to scroll vertically but not horizontally. Do it for y if you want the opposite.

Problem

I want to have only vertical scrolling in my webview and don't want any horizontal scrolling. `webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);` This helped me to solve the scrolling issue. But using this made my webview looking wierd. Height of all the edit text squeezed (Vertically) and is looking bad. Is there any other way I can disable horizontal scrolling from client side? Using SINGLE_COLUMN how can we avoid issue with webview height changes? I want the vertical look and feel to be same as what it used to be without SINGLE_COLUMN

Original source

Related problems