Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Last active August 3, 2021 08:02
Show Gist options
  • Save wajahatkarim3/dcdc8f57aede1b41db3cc6c5f7fe80f0 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/dcdc8f57aede1b41db3cc6c5f7fe80f0 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.common.WebViewActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:max="100" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingEnd="20dp"
android:paddingRight="20dp">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
class WebViewActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val urlToLoad = intent.getStringExtra("url_webview_key")
webView!!.settings.javaScriptEnabled = true
webView.settings.setAppCacheEnabled(true)
webView.loadUrl(urlToLoad)
swipe!!.isRefreshing = true
swipe!!.setOnRefreshListener { webView!!.reload() }
webView!!.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView, progress: Int) {
progressBar!!.progress = progress
if (progress > 99) swipe.isRefreshing = false
}
}
webView!!.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
// Return true if you want to continue loading URL. Otherwise false.
when {
url.contains("/v1/success") -> {
false
}
url.contains("/v1/failure") -> {
false
}
url.contains("v1/cancelled") -> {
false
}
else -> true
}
return isLoad
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
Logs.general(" on Error${error.toString()}")
super.onReceivedError(view, request, error)
isError = true
}
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
Dialogs.dialogSslError(this@HWebViewActivity, error, handler)
}
}
}
override fun onBackPressed() {
if (webView!!.canGoBack()) {
webView!!.goBack()
} else {
finish()
}
}
companion object {
const val WEB_LINK = "WEB_LINK"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment