Reputation: 101
I have a problem , i'm trying to create an app for my website and i'm using android webview to do that, but since there's external links (ads, backlinks.. etc) in my website , when the user clicks these links , the webview opens the links and my app acts like a browser , i know that's what webview is but i want it to open only links of my website
i ovverided the shouldOverrideUrlLoading method and i intercepted the urls and i returned true if the urls are diffrent than my website prefix , but the webview goes all white when i click and external link , and want for the webview is stay the same when i click the external links
here's my code
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
urlData = url;
if (target_url_prefix.equals(host)) {
if (mWebviewPop != null) {
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop = null;
}
return false;
}
if(!url.contains(getString(R.string.target_url))) {
Log.d("intercept External URL", "true");
return true;
}
}
Upvotes: 0
Views: 6116
Reputation: 36
You do not have to write shouldOverrideUrlLoading
method. Only load the url in onCreate()
method.
Check this code.
MainActivity.this.
public class MainActivity extends AppCompatActivity
{
private static final String PAGE_URL = "http://madarabia.com";
private NoInternetDialog noInternetDialog;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
noInternetDialog = new NoInternetDialog.Builder(getApplicationContext()).build();
mWebView = findViewById(R.id.webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.setClickable(true);
// Use remote resource
mWebView.postDelayed(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(PAGE_URL);
}
}, 500);
mWebView.onCheckIsTextEditor();
mWebView.requestFocus(View.FOCUS_DOWN);
mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus())
{
v.requestFocus();
}
break;
}
return false;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
noInternetDialog.onDestroy();
}
// Prevent the back-button from closing the app
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
//If there is history, then the canGoBack method will return ‘true’//
return true;
}
//If the button that’s been pressed wasn’t the ‘Back’ button, or there’s currently no
//WebView history, then the system should resort to its default behavior and return
//the user to the previous Activity//
return super.onKeyDown(keyCode, event);
}
}
Upvotes: 0
Reputation: 21043
In both cases you have consumed the event. try something like below .
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(host)) {
// Intercept URL Load url here return true if url is consumed
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
});
Or if you want to block all other links then you can use it like below .
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(yourwebsite)) {
return super.shouldOverrideUrlLoading(view, request);
}
return true;
}
});
Keep that in mind that all other links will not work so this can be a bad impression on your app . So i suggest that you should open other links with a browser intent . Like below.
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(yourwebsite)) {
return super.shouldOverrideUrlLoading(view, request);
}else{
try {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl()));
startActivity(browserIntent);
}catch (Exception e){
e.printStackTrace();
}
}
return true;
}
});
NOTE:- This is implementation for shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
which is applicable above API 21 . So you should also override shouldOverrideUrlLoading(WebView view, String url)
for previous version in same way .
Upvotes: 3