Skip to content

Instantly share code, notes, and snippets.

@unijad
Last active April 12, 2023 15:34
Show Gist options
  • Save unijad/c58ad42b6816cb8978951fe34205fc1e to your computer and use it in GitHub Desktop.
Save unijad/c58ad42b6816cb8978951fe34205fc1e to your computer and use it in GitHub Desktop.
Android: Stably Ramp with using webkit WebViewWidget and using in-app webview tab activity for external links
<?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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package stably.ramp.example
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import androidx.webkit.WebViewClientCompat
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get a reference to the WebView
val webView: WebView = findViewById(R.id.web_view)
// Configure the WebView settings
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
webSettings.domStorageEnabled = true
webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val settings = webView.settings
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
}
// Enable support for the latest WebView APIs
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON)
}
// Set the WebView client
webView.webViewClient = MyWebViewClient()
// Load a website
webView.loadUrl("https://ramp-beta.stably.io/?integrationId=younis_android_test")
}
private inner class MyWebViewClient : WebViewClientCompat() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
val url = request.url.toString()
// Allow navigation to certain URLs
if (url.contains("stably.io")) {
return super.shouldOverrideUrlLoading(view, request)
}
// Disallow navigation to all other URLs
// and open the URL in a WebView tab within the app
val intent = Intent(this@MainActivity, WebviewTabActivity::class.java)
intent.putExtra("url", url)
startActivity(intent)
// Return true to indicate we're handling the URL ourselves.
return true
}
}
}
package stably.ramp.example
import android.os.Bundle
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
class WebviewTabActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview_tab)
val webView: WebView = findViewById(R.id.web_view)
val extras = intent.extras
if (extras != null) {
val url = extras.getString("url")
if (url != null) {
webView.loadUrl(url)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment