Last active
August 3, 2021 08:02
-
-
Save wajahatkarim3/dcdc8f57aede1b41db3cc6c5f7fe80f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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