androidandroid-layoutandroid-webviewandroid-linearlayoutandroid-websettings

the scrolling is working by reverse on WebView


I used WebView in my app and it sees the scrolling is not working properly, when I trying to scroll down the page does not go down and the reverse is true, but when I try to touch the screen and get off in reverse "touch with scroll up" It's working!!, more clarification in this images gif1 , gif2

code

public class VisitSiteFragment extends android.app.Fragment {

private View view;
private ProgressBar webViewProgressBar;
private WebView webView;
String url;


public VisitSiteFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_visit_site, container, false);
    if (getArguments() != null) {
        url = this.getArguments().getString("url");
        Log.e("URL", url);
    }
    webViewProgressBar = (ProgressBar) view.findViewById(R.id.webViewProgressBar);
    webView = (WebView) view.findViewById(R.id.webView);

    webView.setVisibility(View.VISIBLE);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setUseWideViewPort(true);


    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            webViewProgressBar.setProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }
    });
    webView.setWebViewClient(new WebViewClient() {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            webViewProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webViewProgressBar.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
        }

    });

    webView.loadUrl(url);

    return view;

}

}

the XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VisitSiteFragment"
    android:orientation="vertical"
    android:background="@color/white"
    >

    <ProgressBar
        android:id="@+id/webViewProgressBar"
        android:layout_width="match_parent"
        android:layout_height="8dp"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:theme="@style/progressBarBlue"
        android:layout_gravity="top"
        android:layout_marginTop="-3dp"
        android:progress="20"
        />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </WebView>

</LinearLayout>

I saw someone asking about this problem here, but the answer in that question did not help


Solution

  • After many attempts of WebView settings, I solved the issue by disabling scrolling that embedded with WebView as explained in this answer, and used ScrollView instead of it

    The code after edited

    webView = (WebView) view.findViewById(R.id.webView);
    
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
    
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return (event.getAction() == MotionEvent.ACTION_MOVE);
            }
        });
    
        webView.setScrollContainer(false);
    
    
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                webViewProgressBar.setProgress(newProgress);
                super.onProgressChanged(view, newProgress);
            }
        });
        webView.setWebViewClient(new WebViewClient() {
    
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                webViewProgressBar.setVisibility(View.VISIBLE);
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                webViewProgressBar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
    
        });