Force Android WebView to open in new browser - android

Android has changed its default method for opening clicks, it now opens them in the webview instead of a new browser. This has already been asked here but every thing I have tried opens the links in the WebView. Can someone give me details on capturing the clicks so I con force the link to open in the default browser.

use this in your button onclick:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
For it to parse it needs a http:// at the start.

Finally working don't know if it is the best way but it works. I placed the following code inside the onCreate. The string strSiteUrl set to the page I want the WebView to show.
/* Load WebView in memory */
WebView webv = (WebView) findViewById(R.id.webv);
webv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent();
browserIntent.setAction(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
return false;
}
}); //End webv.setVewView
/* Configure WebView */
WebSettings webSettings = webv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webv.loadUrl(strSiteUrl);
When a user clicked on a link in the WebView page it would open the default browser and show the linked page. However, after clicking the back button depending on which link was clicked the WebView would return to the original page or show the linked page. This is not what I wanted, I only wanted the WebView to show the original page. I don't know why some links didn't return correctly, maybe those links were redirects? So to get around this problem I used the onStart call. I made the view webv global by placing
WebView webv;
in my global declarations. Changed the webv assignment to
webv = (WebView) findViewById(R.id.webv);
Then created the following onStart
#Override
public void onStart() {
super.onStart();
String strReturnUrl = String.valueOf(webv.getUrl());
Log.i("URL!", strReturnUrl);
if (!strReturnUrl.contentEquals(strSiteUrl)) {
webv.loadUrl(strSiteUrl);
}
}
Writing to the log the returned url proved that when the back button was pressed it returned with different urls depending on which link was clicked. I used the if statement to prevent unnecessary reloading of the original url.

Related

Clone/duplicate webview on secondary screen

I have a webview on my MainActivity .. This loaded with a url set by a onclick button handling.
When the webview is loaded with a certain webpage.
I want to clone or duplicate (or even move) it to my secondary display/screen.
For the secondary display i have a seperate Presentation class:
private class BrowserPresentation extends Presentation {
BrowserPresentation(Context ctxt, Display display) { super(ctxt, display); }
WebView webView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView2=new WebView(webView.getContext()); //get webView class of mainactivity
webView2.setWebViewClient(new WebViewClient());
webView2.setInitialScale(100);
setContentView(webView2);
}
}
However this is not working, is it even possible? Am I taking the wrong route? Can't find much on internet about secondary screen handling.
Update:
I don't want to load same page, need to get current page loaded on MainActivity: Want to login on MainActivity then after the login duplicate/move it to second screen(readonly).
As per Android dev page :
Handling Page Navigation
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
That's it. Now all links the user clicks load in your WebView.
If you want more control over where a clicked link load, create your own WebViewClient that overrides the shouldOverrideUrlLoading() method. For example:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// 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;
}
}
Then create an instance of this new WebViewClient for the WebView:
[1]: https://developer.android.com/guide/webapps/webview.html

Android Local app opens in webview

I'm developing an app that should display a web page and from a link in that page open another app, let's call it otherApp. I display the web page in my app with WebView;
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().indexOf("example")>0) {
// 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;
}
});
myWebView.loadUrl("https://www.example.com/demo.html");
The demo.html file contains the link:
Start otherApp
Using the tablet's browser, clicking the link opens otherApp as expected with Chrome still running. However when using my app, otherApp opens in WebView and I need both apps to be running independently of each other. The code was taken from developer.android.com, I'm using a Nexus 7 tablet running Marshmallow.
Any ideas what I am missing?

Android Issue with WebView and overriding the url

Below is the code which we are using to load the WebView. Capturing the url and redirecting to a new activity page is done using the “shouldOverrideUrlLoading”, once the user click on log in button in html page in the webview. But when loading the page, the page is redirecting after the security check(redirecting to https page) and control is coming to the “shouldOverrideUrlLoading” function and its making the activity blank. If we remove the “shouldOverrideUrlLoading” function we can see the log in screen on the WebView. But we are not able to go to the new activity. I tried to catch the redirection url and load it on the
“shouldOverrideUrlLoading” function, but its not allowing to load the content. And I try to return true and false for different conditions form “shouldOverrideUrlLoading” function that also not working.
Can anyone suggest what I need to do to load the log in page in the WebView after the redirecting from the security check and override the url after log in and redirect to a new activity?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
WebView webview = (WebView) findViewById(R.id.wvLogin);
setContentView(webview);
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://Url which needs to override after login"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
}
return true;
}
});
webview.loadUrl("http://Login Page Url");
}

How to avoid webview links opening in the same webview

When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// 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;
}
}
http://developer.android.com/guide/webapps/webview.html
I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions

How to open WebView link to new activity?

How do I open link in webpage to new activity (what contains WebView aswell)?
I have webpage where is list and every list item contain different link. So I want that when user press first item it opens second activity and load that link to second activity's WebView. Hope you understand what I try to ask :)
Is that possible?
You can override the URL link clicks and open an activity on each click:
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Intent intent = new Intent(...);
startActivity(intent);
return true;
}
});

Categories

Resources