Communicating Android Native with WebView

Showing a web widget or web pages as WebView in mobile native. Once a web content is show inside the Android native also communication can be setup.

To enable the web based communication on Android side to download a web page from the internet you need to enable by permission with AnmdroidMenifest.xml file:

<uses-permission android:name=”android.permission.INTERNET“/>

Load html page from external hosted server:

WebView webViewWidget = findViewById(R.id.WebView);
webViewWidget.loadUrl("https://google.com");

Below code will load html page from your folder

webViewWidget.loadUrl("file:///Android_res/raw/some_file.HTML");

Below code will load html Load html page from your default assets folder

webViewWidget.loadUrl("file:///Android_asset/some_file.HTML");

Below code will is to use HTML as string

String _html= "<HTML>"+ "<body><h1>Hello ChatGPT</h1></body>"+ "</HTML>";
webViewWidget.loadData(_html, "text/HTML", "UTF-8");

Note: To use JavaScript, you need to enable it by calling the setJavaScriptEnabled() method on the WebSettings

WebViewWidget webViewWidget = findViewById(R.id.WebView);
WebWidgetSettings webWidgetSettings = webViewWidget.getSettings();
webSettings.setJavaScriptEnabled(true);

WebViewClient

To render a web page WebViewClient call is used and called.

There will be a requirement where you also need to intercept the URL loading here which is possible using the shouldOverrideUrlLoading method.

To listen to the web page events the class WebViewClient helps you like when it starts loading, or finished loading when an error has occurred related to page loading, form submission, links, and other events as well.

WebViewClient Implementation can be as below

private class WidgetWebViewClient extends WebViewClient {
  @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    return super.shouldOverrideUrlLoading(view, request);
  }

  @Override
  public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    super.onReceivedError(view, request, error);
  }

  @Override public void onPageFinished(WebView view, String url) {
  }
}

Next you can set WebViewClient for WebView using the setWebViewClient() method.

webWidgetView.setWebViewClient(new MyWebViewClient());

Enabling Notifications, Console Messages, Warnings, Page Refresh Progress and JavaScript Calls

Using WebChromeClient you can handle JS events. Following is the sample code will demonstrate the approach to get the implementation

private class WidgetWebChromeClient extends WebChromeClient {
  @Override
  public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
    return true;
  }

  @Override
  public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
    return true;
  }

  @Override
  public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
      final JsPromptResult result) {
    return true;
  }
}

Hope the above article written here will be helpful for you and will be part of your bookmark for your future implementations as well.