Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable horizontal scrolling in Android webview

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

like image 474
SK. Avatar asked Jun 16 '12 13:06

SK.


People also ask

How to disable horizontal scroll in WebView Android?

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. But It disabled clicking action in the Webview.

How do I disable scrolling in Webview?

setScrollContainer(false); Don't forget to add the webview. setOnTouchListener(...) code above to disable all scrolling in the webview.

How do I turn off horizontal scrolling?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.

How to hide horizontal scrollbar in Android?

Putting android:scrollbars="none" on the HorizontalScrollView element has no effect. as mentioned by @Benito, do it programmatically.


1 Answers

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.

like image 173
vee Avatar answered Sep 28 '22 15:09

vee