Reputation: 33
hello i have a webview android app. i want that my website open in webview only and if somone click on a url that is not related to my website then open in browser. Example: if my website is little.com and if some one clcik a link that is facebook.com or google.com then facebbok or google open in browser.
androidmainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.little.example">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- The below is for the splash screen and we need no action bar and the default theme -->
<activity android:name=".homeActivity"
android:theme="@style/AppTheme.NoActionBar"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name=".MainActivity">
</activity>
</application>
</manifest>
mainactivity.kt
package com.little.example
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.webkit.WebBackForwardList
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.little.example.R
import kotlinx.android.synthetic.main.activity_main.*
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webview.settings.javaScriptEnabled = true
webview.loadUrl("https://little.com")
webview.webViewClient = MyWebViewClient()
}
override fun onBackPressed() {
if (webview.canGoBack()){
webview.goBack()
}
else {
super.onBackPressed()
}
}
private inner class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (!url.contains("little.com")) {//for example
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
view.loadUrl(url)
return false
}}
}
Upvotes: 1
Views: 1595
Reputation: 806
you can handle every url as you wish just set up your webview via customized WebViewClient
mWebView.webViewClient = MyWebViewClient()
}
private inner class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (!url.contains("little.com")) {//for example
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
mWebView.loadUrl(url)
return false
Upvotes: 3
Reputation: 1
By using custom tabs in androidx
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(context,Uri.parse("http://www.google.com"));
Upvotes: -1
Reputation: 795
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(browserIntent);
you can use this code to open a page in a browser
Upvotes: 1