Category: WebView

  • Send event of scrolling from ScrollView to the WebView JavaScript code

    GitHub: https://github.com/Pulimet/ScrollViewEventTest

    First in onCreate() we will set our WebView:

    private WebView mWebView;
    private ScrollView mScrollView;
    public static final long DELAY_TIME = 1000;
    private long mTime;
    ...
    mWebView = findViewById(R.id.webView);
    mWebView.loadUrl("http:///alex.srv1043500.hstgr.cloud");
    

    Enable JavaScript:

    WebSettings webViewSettings = mWebView.getSettings();
    webViewSettings.setJavaScriptEnabled(true);
    

    Create a method that would invoke our JavaScript code:

    private void invokeJavaScriptCode(String code) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mWebView.evaluateJavascript(code, null);
      } else {
        mWebView.loadUrl(String.format("javascript:%s", code));
      }
    }
    

    (more…)