First you must have the permission to connect to the internet:
<uses-permission android:name="android.permission.INTERNET"/>
Now I do not see that you get the reference of WebView
within the layout activity_main.xml
.
You must have the WebView
and you must obtain the reference.
As an example, assuming that your WebView has the id webview
:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
In this way you would load the url within WebView
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Obtiene referencia de la vista.
var webView = findViewById<WebView>(R.id.webview)
val settings = webView.settings
settings.javaScriptEnabled = true
webView.loadUrl("https://www.venze.es/terms-conditions/")
}
Kotlin, how to load an unencrypted URL in a WebView.
Now another important detail, the url that you sample is not encrypted:
I imagine they have a balancer, sometimes it shows that it has a certificate and in others it does not.
Therefore you need to create a class that extends from WebViewClient
and about writing the method:
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
handler.proceed() // Ignora errores de certificado SSL
}
Finding an error ignores any SSL certificate error and continues loading.
This would be an example of the code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Obtiene referencia de la vista.
var webView = findViewById<WebView>(R.id.webview)
val settings = webView.settings
settings.javaScriptEnabled = true
//Define WebViewClient
webView.webViewClient = HelloWebViewClient()
//Carga url
webView.loadUrl("https://www.venze.es/terms-conditions/")
}
}
class HelloWebViewClient : WebViewClient() {
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
handler.proceed() // Ignora errores de certificado SSL
}
}