Android - Open target _blank links in WebView with external browser
After visiting the above links, I come up with this code and hope this helps.
wv.getSettings().setSupportMultipleWindows(true);
wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
{
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
Context context = view.getContext();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
context.startActivity(browserIntent);
return false;
}
});
webview open target _blank in new fragment tab
Finally a little tips from at the bottom of this question
Android - Open target _blank links in WebView with external browser
mWebView.getSettings().
setSupportMultipleWindows(true);
@Override
public boolean onCreateWindow(WebView
view, boolean dialog, boolean userGesture, Message
resultMsg)
{
WebView newView = new
WebView(getActivity());
WebSettings settings =
newView.getSettings();
settings.setJavaScriptEnabled(true);
settings.
setJavaScriptCanOpenWindowsAutomatically(true);
settings.
setSupportMultipleWindows(true);
settings.setUseWideViewPort(false);
newView.setWebViewClient(new
WebViewClient() {
@Override
public boolean
shouldOverrideUrlLoading(WebView view, String urls) {
models.add(models.size() - 1,
new WebpageModel(DEFAULT_TITLE,
urls));
navDrawerAdapter.notifyDataSetChanged();
fragments.
add(WebpageFragment.newInstance(fragments.size(),
models.get(models.size() - 2).getUrl()));
getActivity().
getSupportFragmentManager().beginTransaction()
.hide(fragments.get(current_position))
.add(R.id.container,
fragments.get(fragments.size() - 1))
.commit();
current_position =
fragments.size() - 1;
return true;
}
});
WebView.WebViewTransport transport =
(WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newView);
resultMsg.sendToTarget();
return true;
}
but when i press download button in getintopc.com unlike other browser it will wait few seconds and create new tab and give me the file to be download instantly instead of going to processing download and wait few seconds page.
for other site with target _blank works fine!
Android WebView links to same window with target=_blank to open new window
First,
mWebView.getSettings().setSupportMultipleWindows(true);
Then in WebChromeClient
, override OnCreateWindow()
private class MyWebChromeclient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(WebViewActivity2.this);
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
return true;
}
});
return true;
}
Related Topics
Google Map for Android My Location Custom Button
Android Studio Could Not Find Any Version That Matches Com.Android.Support:Appcompat-V7:+
Does Android Service Run from a Separated Thread Instead of UI
How to Create an Android Spinner as a Popup
Using Onbackpressed() in Android Fragments
How to Resolve Target 'Android-16'
Change Navigation Bar Icon Color on Android
Retain The Fragment Object While Rotating
What Are Intent-Filters in Android
End Incoming Call Programmatically
How to Fix Android Studio Getting Stuck Executing Gradle Tasks
How to Enable Internal App Sharing for Android
Phone Number Validation Android
Disable Webview Touch Events in Android