I have a WebView in my app. It works properly but at Api 20, WebView does not show the content.
I checked the logs and see these errors
I/chromium: [INFO:CONSOLE(7)] "The key "shrink-to-fit" is not recognized and ignored.", source: https://...
I/chromium: [INFO:CONSOLE(43)] "Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode", source: https://... (43)
I overviewed some answers and they generally suggest to use
webView.settings.useWideViewPort = true
webView.settings.loadWithOverviewMode = true
I have already implement these settings but still same error happens. WebView shows noting
Here is my WebView Settings
/**
* Initialize the WebView
*/
#SuppressLint("SetJavaScriptEnabled")
private fun initWebView() {
webView.webViewClient = object : WebViewClient() {
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return super.shouldOverrideUrlLoading(view, request)
}
override fun onPageStarted(
view: WebView, url: String, favicon: Bitmap?) {
showProgressDialog()
}
override fun onPageFinished(view: WebView, url: String) {
hideProgressDialog()
}
#RequiresApi(api = Build.VERSION_CODES.M)
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
// Your code to do
hideProgressDialog()
}
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
// Handle the error
hideProgressDialog()
}
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler?, error: SslError) {
// ignore ssl error
if (handler != null) {
handler.proceed()
} else {
super.onReceivedSslError(view, null, error)
}
}
}
webView.isHorizontalScrollBarEnabled = false
webView.settings.javaScriptEnabled = true
webView.settings.useWideViewPort = true
webView.setInitialScale(1)
webView.settings.loadWithOverviewMode = true
context?.let { webView.setBackgroundColor(ContextCompat.getColor(it, R.color.colorDarkBlue)) }
}
/**
* Load url
*/
private fun loadPage(url: String) {
webView.loadUrl(url)
}
Someone have a idea? I will be appreciate for any help
Related
I have a WebView in my app. But when the website behavior is to open a modal, the webview redirects me to a blank screen. I put logs in onPageStarted and shouldOverrideUrlLoading, and saw that it doesn't fit in either method.
webview.settings.javaScriptEnabled = true
webview.settings.domStorageEnabled = true
webview.settings.javaScriptCanOpenWindowsAutomatically = true
webview.settings.setSupportMultipleWindows(true)
webview.settings.allowFileAccess = true
webview.setWebChromeClient(object : WebChromeClient() {
#SuppressLint("SetJavaScriptEnabled")
override fun onCreateWindow(
view: WebView?,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message
): Boolean {
val newWebView = WebView(context)
val webSettings = newWebView.settings
webSettings.javaScriptEnabled = true
// Other configuration comes here, such as setting the WebViewClient
val dialog = Dialog(context!!)
dialog.setContentView(newWebView)
dialog.show()
newWebView.setWebChromeClient(object : WebChromeClient() {
override fun onCloseWindow(window: WebView) {
dialog.dismiss()
}
})
(resultMsg.obj as WebViewTransport).webView = newWebView
resultMsg.sendToTarget()
return true
}
})
webview.setWebViewClient(object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
override fun shouldOverrideUrlLoading(webView: WebView, url: String): Boolean {
webView.loadUrl(url)
return true
}
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
Handler().postDelayed({
if(progressBar!=null)
progressBar.visibility = View.GONE
}, 3500)
}
})
webview.loadUrl(url)
I also tried to redirect it to the chrome client if it was going to open the modal, but as it doesn't load any new urls, I don't think it's overwriting the method
I have a link to the PDF or XML file. By default, a web view doesn't render such links. From another StackOverflow post, I figured out I need to wrap by link by google doc or google drive link (http://docs.google.com/viewer?url=myPDFLink). It works, the web view displays content but not in all cases. For the same link, the content randomly might be not displayed or be displayed. If the page is not displayed I can just retry several times and content displays. Cannot find a reason for such behavior. Even if the web view is blank, the onPageFinished callback is being called but not any error callbacks.
Update: onPageStarted is not being called but onPageFinished is being
WebViewClient:
val webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
updateOnBackPressedCallbackState()
toolbarActivity?.setToolBarTitle(url)
onPageStateListener?.onPageStarted(view, url)
}
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
progressBar?.visibility = View.GONE
toolbarActivity?.setToolBarTitle(view.title)
onPageStateListener?.onPageFinished(view)
}
override fun onReceivedError(
view: WebView?,
errorCode: Int,
description: String?,
failingUrl: String?
) {
super.onReceivedError(view, errorCode, description, failingUrl)
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
}
override fun onReceivedHttpError(
view: WebView?,
request: WebResourceRequest?,
errorResponse: WebResourceResponse?
) {
super.onReceivedHttpError(view, request, errorResponse)
}
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
super.onReceivedSslError(view, handler, error)
}
}
Settings:
webViewer.webViewClient = webViewClient
webViewer.settings.javaScriptEnabled = true
webViewer.isHorizontalScrollBarEnabled = true
webViewer.settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
Try to handle SSL errors within onReceivedSslError to proceed with the certificate
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) =
if (handler != null) {
handler.proceed()
} else {
super.onReceivedSslError(view, handler, error)
}
I wanna provide a custom browser inside app to the user could pass authorization on GitHub.com. But Authorize button is disabled in WebView.
I set recommended flags from other questions on stackoverflow but it didn't help. There is my ViewModel below where you can see flags and clients that I set for WebView:
class LoginViewModel : ViewModel() {
var url: ObservableField<String> = ObservableField()
var isLoading: ObservableBoolean = ObservableBoolean()
val webViewClient: WebViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
override fun onReceivedError(
view: WebView, request: WebResourceRequest,
error: WebResourceError
) {
super.onReceivedError(view, request, error)
isLoading.set(false)
}
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
isLoading.set(false)
}
}
val webChromeClient: WebChromeClient = WebChromeClient()
val webViewConfig: IWebViewConfig = object : IWebViewConfig {
override fun onConfig(webView: WebView) {
webView.settings.domStorageEnabled = true
webView.settings.databaseEnabled = true
webView.settings.javaScriptEnabled = true
webView.settings.loadWithOverviewMode = true
webView.settings.useWideViewPort = true
webView.settings.allowContentAccess = true
webView.settings.allowFileAccess = true
webView.settings.allowFileAccessFromFileURLs = true
webView.settings.allowUniversalAccessFromFileURLs = true
webView.settings.userAgentString = "Android"
}
}
fun onRefresh() {
isLoading.set(true)
url.notifyChange()
}
}
Where can the problem be?
I have tried it on android 7.0, it works, but on android 5,6 it does not work, i could not find any clue on what maybe going wrong.
The URL redirect to payment portal and at the end it calls Android.postMessage("success") from Javascript, but it does not get called in native this problem only persist in android 5 and maybe on android 6 too.
My minimum SDK level is 21.
class PaymentActivity : BaseActivity<ActivityPaymentBinding, IPaymentMvvm.ViewModel>(), IPaymentMvvm.View {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
setAndBindContentView(savedInstanceState, R.layout.activity_payment)
viewModel.initVM()
binding.web.webViewClient = Web(viewModel)
binding.web.settings.javaScriptEnabled = true
binding.web.settings.loadWithOverviewMode = true
binding.web.settings.useWideViewPort = true
binding.web.addJavascriptInterface(JSBridge(this, this), "Android")
val url = intent.extras.getString("url")
if (url == null)
finish()
binding.web.loadUrl(url)
}
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
android.R.id.home ->
finish()
}
return super.onOptionsItemSelected(menuItem)
}
class Web(val viewModel: IPaymentMvvm.ViewModel) : WebViewClient() {
init {
viewModel.setProgress(true)
}
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
viewModel.setProgress(false)
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
super.onReceivedError(view, request, error)
viewModel.setProgress(false)
Timber.e(error.toString())
}
}
}
class JSBridge(val context: Context, val activity: Activity) {
#JavascriptInterface
fun postMessage(message: String) {
// here we return true if we handled the post.
Timber.i(message)
context.toast(message)
val intent = Intent(context, OrderThanksActivity::class.java)
intent.putExtra(Enums.contactDetail.contactNumber.name, activity.intent.getStringExtra(Enums.contactDetail.contactNumber.name))
intent.putExtra(Enums.contactDetail.phoneNumber.name, activity.intent.getStringExtra(Enums.contactDetail.phoneNumber.name))
intent.putExtra(Enums.contactDetail.cellNumber.name, activity.intent.getStringExtra(Enums.contactDetail.cellNumber.name))
activity.startActivity(intent)
activity.finish()
}
}
It was the problem of JavaScript script from web, which threw an error in a script running in web view before my interface function even get called, but the weird behavior was that script only got terminated on Android 5,6 while Android 7 web view does not terminate execution and execute the whole script regarding error that's why the interface function got called in Android 7.
Solution must work on API >20
This solution does not work. I assume it works only on lower versions of the Android API. I'm testing on Android 8.0.
I've tried using Retrofit as soon as the page is loaded, instead of getting the html via WebViewClient. But the user is not logged in in the Retrofit request. And I'm doing the same request 2 times then.
#SuppressLint("JavascriptInterface")
private fun initializeWebView(url : String?) {
binding.webView.loadUrl(url)
binding.webView.settings.javaScriptEnabled = true
binding.webView.settings.useWideViewPort = true
binding.webView.requestFocus(View.FOCUS_DOWN)
binding.webView.addJavascriptInterface(MyJavaScriptInterface(context!!), "HtmlViewer");
binding.webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return false
}
override fun onPageFinished(view: WebView?, url: String?) {
// super.onPageFinished(view, url)
//tried adding a sleep, but doesn't work:
Thread.sleep(6000)
//showHTML method is not being called:
binding.webView.loadUrl("javascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');")
// Prints out: html: null
binding.webView.evaluateJavascript(
"javascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
) { html ->
Log.d("HTML", html)
// code here
}
}
}
}
class MyJavaScriptInterface(val context: Context) {
fun showHTML(html: String) {
Log.d("",""+html)
AlertDialog.Builder(context).setTitle("HTML").setMessage(html)
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show() }
}