How to disable Vertical scrolling of webview to load whole page as different column

13,017

Solution 1

You can try single column layout

myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

or in XML you can put none in scrollbar

<WebView
    android:scrollbars="none" />

then set myWebView.setScrollContainer(false);

Solution 2

first you have to add custom webview for disable vertical scroll down

class CustomWebView2 extends WebView
{
    Context mContext = null;
    private float start_y = -1;
    private String DEBUG_TAG = "  touch:";

    public CustomWebView2(Context context)
    {
        super(context);
        mContext = this.getContext();
    }

    public CustomWebView2(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();

        switch (action)
        {
            case (MotionEvent.ACTION_DOWN):
                /**
                 * get Y position
                 */
                start_y = event.getY();
                break;
            case (MotionEvent.ACTION_MOVE):
                /**
                 * if you scroll to up more than 60px
                 * webView scroll back to Y=0
                 */
                if(start_y-event.getY()>60)
                {
                    scrollTo(getScrollX(),0);
                    return true;
                }
                break;
            case (MotionEvent.ACTION_UP):
                Log.d(DEBUG_TAG, "Action was UP");
                break;
        }
        return super.onTouchEvent(event);
    }
}

then you can create multi columns webview with javascript (https://stackoverflow.com/a/9165698/1140304)

You have to apply these lines to the setting of webview

 CustomWebView webView_ = (CustomWebView) view.findViewById(R.id.webView);
 webView_.getSettings().setUseWideViewPort(true);
 webView_.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
 /**
 * hide vertical scrollBar
 */
 webView_.setVerticalScrollBarEnabled(false);

after your page completely loaded on webview , you have to run javascript code on your webview

 webView_.setWebViewClient(new WebViewClient(){

            public void onPageFinished(WebView view, String url) {
                injectJavascript();
            }

        });

this is javascript code

public void injectJavascript() {
        String js = "javascript:function initialize() { " +
                "var d = document.getElementsByTagName('body')[0];" +
                "var ourH = window.innerHeight; " +
                "var ourW = window.innerWidth; " + 
                "var fullH = d.offsetHeight; " +
                "var pageCount = Math.floor(fullH/ourH)+1;" +
                "var currentPage = 0; " +
                "var newW = pageCount*ourW; " +
                "d.style.height = ourH+'px';" +
                "d.style.width = newW+'px';" +
                "d.style.webkitColumnGap = '2px'; " +
                "d.style.margin = 0; " +
                "d.style.webkitColumnCount = pageCount;" +
                "}";
        webView_.loadUrl(js);
        webView_.loadUrl("javascript:initialize()");
    }

Solution 3

Here a simple but effective solution, the only working for me (reference here):

body{
    position:fixed;
    top:0;
}
Share:
13,017
Dev.Sinto
Author by

Dev.Sinto

Android programming

Updated on June 29, 2022

Comments

  • Dev.Sinto
    Dev.Sinto almost 2 years

    I want to disable webview vertical scroll view.Load the web page as columns in webview by using this way I can give the user a book reading effect.

    this is the style I added in html.it is not working at all.

      loadingStringHorizontal="<style type='text/css'>body { width:200px;height:400px;
        -webkit-column-gap:17px;-webkit-column-width:170px;
         text-align:justify ;} </style>";
    

    Thanks for your time.

  • Satan Pandeya
    Satan Pandeya almost 7 years
    Add some description too rather than dumping the code only.
  • Roel
    Roel almost 7 years
    SINGLE_COLUMN is deprecated