Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example.tortotrulo
- import android.content.Intent
- import android.net.Uri
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.webkit.WebView
- import android.webkit.WebViewClient
- import androidx.core.content.ContextCompat
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- //a WebViewClient that accepts all www.cake.co URLs and displays them, but relegates
- //other URLs to the system's default browser
- private class CakeWebViewClient : WebViewClient() {
- override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
- if (Uri.parse(url).host == "www.cake.co") {
- //false for all URLs under the cake.co host, these will open in app
- return false
- }
- //external link, launch another Activity that handles URLs
- if (view?.context != null) {
- Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
- ContextCompat.startActivity(view.context,this,null)
- }
- }
- return true
- }
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //use a WebViewClient that opens all cake.co URLs in the view,
- //but opens external web content elsewhere (e.g. Chrome)
- webview.webViewClient = CakeWebViewClient()
- //enable JS for functionality like Follow/Signin to work
- webview.settings.javaScriptEnabled = true
- //load some cake.co content
- webview.loadUrl("https://www.cake.co/")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement