kotlin webview example
Andro Development is the Android developer platform to teach Android programming with online education practices on Android use languages, for instance, coffee. Andro growth is for technical grade developers and with regard to novices and has focused on effortless. We have simple and positive code example through which the audience will undoubtedly take it. The instructional practices of The phase start from beginners point and go to expert level.
Kotlin features highly simplify Android development not only by allowing you to have smaller structures of text, but maintaining semantically good functions in the app. That would allow you to think less about implementation information and more about the entire app system. Practical role of Kotlin can reduce side effects of the code and make it easier to check and see.
1.WebActivity.kt:
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.http.SslError
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.SslErrorHandler
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_web.*
class WebActivity : AppCompatActivity() {
companion object {
const val PAGE_URL = "pageUrl"
const val MAX_PROGRESS = 100
fun newIntent(context: Context, pageUrl: String): Intent {
val intent = Intent(context, WebActivity::class.java)
intent.putExtra(PAGE_URL, pageUrl)
return intent
}
}
private lateinit var pageUrl: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web)
// get pageUrl from String
pageUrl = intent.getStringExtra(PAGE_URL)
?: throw IllegalStateException("field $PAGE_URL missing in Intent")
initWebView()
setWebClient()
handlePullToRefresh()
loadUrl(pageUrl)
}
private fun handlePullToRefresh() {
}
@SuppressLint("SetJavaScriptEnabled")
private fun initWebView() {
webView.settings.javaScriptEnabled = true
webView.settings.loadWithOverviewMode = true
webView.settings.useWideViewPort = true
webView.settings.domStorageEnabled = true
webView.webViewClient = object : WebViewClient() {
override
fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
handler?.proceed()
}
}
}
private fun setWebClient() {
webView.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(
view: WebView,
newProgress: Int
) {
super.onProgressChanged(view, newProgress)
progressBar.progress = newProgress
if (newProgress < MAX_PROGRESS && progressBar.visibility == ProgressBar.GONE) {
progressBar.visibility = ProgressBar.VISIBLE
}
if (newProgress == MAX_PROGRESS) {
progressBar.visibility = ProgressBar.GONE
}
}
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// Check if the key event was the Back button and if there's history
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack()
return true
}
// If it wasn't the Back key or there's no web page history, exit the activity)
return super.onKeyDown(keyCode, event)
}
private fun loadUrl(pageUrl: String) {
webView.loadUrl(pageUrl)
}
}
2.activity_web.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:minHeight="4dp"
android:padding="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
No comments:
Post a Comment