Android (Kotlin)

Check out some examples of how to set up deeplinks and listen for webview redirects in Android 🤖.

Prerequisites

In order to be able to use the hosted widget in your Android app, make sure that you:

Set up deeplinks in your Android application

In your AndroidManifest.xml file, add the following code:

<manifest ...>
  <!-- ... -->
<application ...>
    <activity ...>
      <!-- ... -->
            <intent-filter> <!-- You need to add this entire XML object. -->
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with belvowidget:// -->
                <data
          android:scheme="belvowidget" 
          android:host="" /> 
      </intent-filter>
    </activity>
  </application>
</manifest>

Handle events in your webview

In the code sample below you can see an example of how to listen and handle for events within your webview.

val belvoWebView: WebView = findViewById(R.id. WEBVIEW_ID_ASSIGNED_IN_CONTENT  )
        belvoWebView.loadUrl("https://widget.belvo.io/?access_token={access_token_from_backend}")
        belvoWebView.settings.javaScriptEnabled = true
        belvoWebView.settings.allowContentAccess = true
        belvoWebView.settings.setDomStorageEnabled(true)
        belvoWebView.settings.useWideViewPort = true
        belvoWebView.settings.setAppCacheEnabled(true)


        belvoWebView.webViewClient = object : WebViewClient () {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
                view?.loadUrl(url)
               if (url.startsWith("https")) {
                   return true
               }
               else {
                   belvoWebView.stopLoading()
                   val uri: Uri = Uri.parse(url)
                   val host: String? = uri.host
                   if (host == "success") {
                       val link : String? = uri.getQueryParameter("link")
                       val institution : String? = uri.getQueryParameter("institution")
                       // Do something with the link and institution.
                   }
                   else if (host == "exit") {
                     // If the redirect starts with "exit",
                     // Do something with the data.
                   }
                   else {
                     // If the redirect starts with "error",
                     // Do something with the data.
                   }
                   belvoWebView.goBack()
               }
               return false
            }
        }

Did this page help you?