I am working on an Android project using Jetpack Compose, and I need to display a TradingView chart within a WebView. However, I’m encountering an issue where the TradingView URL does not load in the Jetpack Compose WebView, while other URLs load without any problems. Interestingly, the TradingView URL works fine when used in XML-based project.
item {
val symbol = selectedCoin?.symbol
val url =
"https://s.tradingview.com/widgetembed/?frameElementId=tradingview_76d87&symbol=${symbol}USD&interval=D&hidesidetoolbar=1&hidetoptoolbar=1&symboledit=1&saveimage=1&toolbarbg=F1F3F6&studies=[]&hideideas=1&theme=Dark&style=1&timezone=Etc/UTC&studies_overrides={}&overrides={}&enabled_features=[]&disabled_features=[]&locale=en&utmsource=coinmarketcap.com&utm_medium=widget&utm_campaign=chart&utm_term=BTCUSDT"
TradingChartWebView(url = url)
}
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun TradingChartWebView(url: String) {
val context = LocalContext.current
AndroidView(
factory = {
WebView(context).apply {
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
loadWithOverviewMode = true
useWideViewPort = true
cacheMode = WebSettings.LOAD_NO_CACHE
mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
userAgentString =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
webChromeClient = WebChromeClient()
webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
Log.d("WebView", "Page started loading: $url")
}
override fun onPageFinished(view: WebView?, url: String?) {
Log.d("WebView", "Page finished loading: $url")
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
Log.e("WebView", "Error loading page: ${error?.description}")
}
}
loadUrl(url)
}
},
modifier = Modifier
.fillMaxWidth()
.height(400.dp)
)
}
Here I am trying to show the chart of the cryptocurrency with the tradingview url, but I get a blank screen like this
I tried the codes I shared above and I did not get a webview graphic. I expect a webview chart to appear.