javaandroidkotlinandroid-studioandroid-webview

Android WebView can't load web page, and returns white screen


I'm trying to create a simple application using WebView, which should simply open the site "google.com"

However, instead of the site, I just get a white screen, without any errors, etc.

Can you please see what I did wrong?

My code:

MainActivity

private fun initWebView() {

        binding.mainWebView.webViewClient = MyWebClient()
        binding.mainWebView.settings.javaScriptEnabled = true
        binding.mainWebView.settings.cacheMode = WebSettings.LOAD_DEFAULT
        binding.mainWebView.settings.domStorageEnabled = true
        binding.mainWebView.settings.blockNetworkImage = false
        binding.mainWebView.settings.blockNetworkLoads = false
        binding.mainWebView.settings.loadsImagesAutomatically = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            binding.mainWebView.settings.safeBrowsingEnabled = false
        }
        binding.mainWebView.settings.javaScriptCanOpenWindowsAutomatically = true
        binding.mainWebView.settings.allowFileAccess = true
        binding.mainWebView.settings.useWideViewPort = true
        binding.mainWebView.fitsSystemWindows = true
        binding.mainWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null)

        binding.mainWebView.loadUrl("https://google.com")

    }

MyWebClient



class MyWebClient : WebViewClient() {
    val TAG = "===MyWebClient"
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        view!!.loadUrl(request!!.url.toString())
        return true
    }

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        view!!.loadUrl(url!!)
        return true
    }


    override fun onReceivedHttpError(
        view: WebView?,
        request: WebResourceRequest?,
        errorResponse: WebResourceResponse?
    ) {
        Log.i(TAG, "onReceivedHttpError: http error ")
        super.onReceivedHttpError(view, request, errorResponse)
    }

    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?
    ) {
        Log.i(TAG, "onReceivedError: ${error!!.errorCode}")
        Log.i(TAG, "onReceivedError: ${error.description}")
    }

    override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
        handler!!.proceed()
        Log.i(TAG, "onReceivedSslError: ${error}")
    }

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        Log.i(TAG, "onPageStarted: started ${url}")
        super.onPageStarted(view, url, favicon)

    }


    override fun onPageFinished(view: WebView?, url: String?) {
        Log.i(TAG, "onPageFinished: finished ${url}")
        super.onPageFinished(view, url)
    }
}

Android Manifest

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


<application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"
        ...
/>

network_security_config

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">google.com</domain>
        <domain includeSubdomains="true">www.google.com</domain>
   
    </domain-config>
</network-security-config>

I use clearTextTraffic, and Network Security, as well as various examples from the Internet, but nothing helps me...

Everything is fine with the Internet, requests using Retrofit2 are processed smoothly

Also, the onReceivedError, onReceivedSslError, and onReceivedHttpError not cathced

Only onPageStarted and onPageFinished works


Solution

  • I found what the problem was!

    Initially, my application is built on Compose, and I tried to use WebView from View.

    I used an example from here:

    Android WebView with Jetpack Compose not rendering Mantine UI (React) components properly

    and it worked successfully