Launching a open a browser when clicking a link in android webview - java

I'm trying to make a link inside my android webview load into one of the web browsers available onto the device. What is happening is that it just loads the link inside the webview. How can I make the link load outside the webview of my application?
The link that I am need to make work is this:
Forgot your password?
When I click it nothing happens, I need it to call the default web browser of the device.
My shouldOverrideUrlLoading is currently being used to check the URL and loads another activity if it is equal:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("extraData", "the urls"+Uri.parse(url).getPath());
if (Uri.parse(url).getPath().equals("/qr_code")) {
progress.setVisibility(View.GONE);
Intent intent = new Intent(getApplicationContext(),QRActivity.class);
startActivityForResult(intent, 1);
setContentView(R.layout.activity_qr);
//return true;
}
if(Uri.parse(url).getPath().equals("/linka")){
return true;
}
return false;
}
Update: It is now working but for some reason my current webView still runs the link therefore the pages loads to the link clicked
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("extraData", "the urls"+Uri.parse(url).getPath());
if (Uri.parse(url).getPath().equals("/TMMobile/Main/qr_code")) {
//return true;
}
if(Uri.parse(url).getPath().equals("/linka")){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://testdomain/RecoverPassword.aspx"));
startActivity(browserIntent);
}
return false;
}
}

To open a URL in a browser, just can use the following code:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

Related

How can I open multiple PDF Links in Android WebView in Android?

I'm building android app where, I placed links to the page in webview on which multiple links are there pointing to PDFs. Everyday, new links are updated. I want to make such functionality that when user clicks on the particular PDF link, it should get opened in default PDF viewer or there itself.
Right now, nothing is happening when user clicks on PDF link within webview.
WebView w=(WebView)findViewById(R.id.web1);
WebSettings webSettings=w.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
w.setWebViewClient(new WebViewClient());
w.loadUrl("http://collegecirculars.unipune.ac.in/sites/examdocs/_layouts/mobile/view.aspx?List=7ed6607e-6c43-401a-a922-bf8d8bf08ed8&View=dc261157-c533-4a60-977b-506fd87b2a19");
You can use Google Docs Viewer to read your pdf online:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://collegecirculars.unipune.ac.in/sites/examdocs/Examiantions%20Timetables/Master-In-ARTS(M.A)(Rev.2008)-(24908)_Spl4-8-16.pdf?Mobile=1&Source=%2Fsites%2Fexamdocs%2F_layouts%2Fmobile%2Fview%2Easpx%3FList%3D7ed6607e%252D6c43%252D401a%252Da922%252Dbf8d8bf08ed8%26View%3Ddc261157%252Dc533%252D4a60%252D977b%252D506fd87b2a19%26ViewMode%3DSimple%26CurrentPage%3D1";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
open in browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
To override current URL :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// here you can check PDF
url = url.toLowerCase();
view.loadUrl(url);
return true;
}
}
set this MyWebViewClient to your webView.
PaymentWebViewClient paymentWebViewClient = new PaymentWebViewClient();
webView.setWebViewClient(paymentWebViewClient);

Webview tries to open all links, but it should be opened in a browser

I am having a webview, the webview might contain a webpage with two different types of links. What I want is that, if the user clicks on a link which contains http://webpagename.com it should open the link in the phone's browser. If the user clicks on the second type of link, which does not have http:// he should be redirected to a new activity. Right now what is happening is that, the links with http:// are opening up both in webviews and browsers. But, the links not having http:// is showing Web Page not available.
The code to handle onclick link in webview:
// to know which link is clicked
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return null;
}
else {
Log.e("URL: ", url);
System.out.println(url.replace(url.substring(0,url.lastIndexOf("/")+1), ""));
return null;
}
}
/**
* Return WebResourceResponse with CSS markup from a String.
*/
});
return super.shouldOverrideUrlLoading(view, url);
}
});
What should I do to make sure the http:// links always opens in the browser and the other links open an activity but nothing opens in the webview?
From the docs for WebViewClient, the shouldOverrideUrlLoading() method:
Returns
True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
Since you're handling the WebView's content and the redirects to Activities, this method should explicitly return true, rather than the super method's return value.
From your description and code, it doesn't appear that you need to override the shouldInterceptRequest() method.
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url != null && url.startsWith("http://"))
{
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
else
{
Log.e("URL: ", url);
System.out.println(url.replace(url.substring(0, url.lastIndexOf("/") + 1), ""));
// Start your app's Activity here
}
return true;
}
});

Android Display Web View

Hi I'm working on an android application that uses a web view. It works perfectly on most API levels, but I'm having problems on API 9 (Gingerbread 2.3.5). Where it's really inconsistent to whether it works or not. I've used the following code for it to work on froyo:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(urlString)) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
but this doesn't get hit when debugging on API level 9 (Gingerbread 2.3.5). Has anyone else had this issue? if so what was causing it and how did you fix it?
Thanks

webview never call ShuldOverrideUrlloading in webviewclient in android

I have a webview and a link on the URL that I have loaded on that webview, I want such like that if user clicks in this link the app will go to the background and open that link with the default web browser of that device. To do this I have done the following:
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.getSettings().setPluginState(PluginState.ON);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null
&& url.startsWith("http://the6figuremarketer.com/apps/androidfb21/thks.html")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
web.loadUrl("http://the6figuremarketer.com/apps/androidfb21/index.html");
but I figured out that the shouldoverrideurl method is nevet get called. what should I do now? can anyone help?
Look at shouldOverrideUrlLoading in WebView for Android not running.
shouldOverrideUrlLoading() is not called when you call loadurl on webview. It is called only when you click on a link in webview.

Creating An In-Game Web Browser on Android

In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".

Categories