Open webview in separate browser on each click? - android

I am preparing sample app based on Web View. In my Splash screen have to load on url,if i click on splash screen it will open another site.It is working fine.But when i click on splash screen i want open separate browser.For that i have used following code,
this.webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
But it is opening same browser.Please Guide me.

view.loadUrl(url) will open the url in the same WebView. You have to define rule when to load in the same WebView or another seperate WebView or Browser. To load in seperate webview call anotherWebView.loadUrl(url). Or browser call with Intent.ACTION_VIEW.
The workaround could be like this-
if(need to load same webView)
{
view.loadUrl(url);
}
else if(need to load same webView)
{
anotherWebView.loadUrl(url);
}
else
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

Related

Android webview open external links in own application

When external links are clicked on my website, I want them to open in the application it is connected to. But I am getting page not found error. For example, when instagram.com is clicked, it will open on Instagram.
You need to define a webView CLient like this:
private class MyWebViewClient extends WebViewClient {
//link opener
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("yourHost.com".equals(Uri.parse(url).getHost()))
{
// This is my website, so do not override; let my WebView load the page
//loading.show();
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;
}
}
on set this webViewClient in your webView like: mywebView.setWebViewClient(new MyWebViewClient())

How can i make a link to open in browser instead of WebView App

I created a WebView App and it's working fine but there is a link which I want it to open in the default browser instead of that app what can I do.
Add a WebViewClient to your webView (if not already added) and then override shouldOverrideUrlLoading () method:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
if (url.contains(myUrl)) {
Intent intent= new Intent(Intent.ACTION_VIEW, myUrl);
context.startActivity(intent);
return true;
} else {
return super.shouldOverrideUrlLoading(webview, url);
}
}
}
This way your are telling your webview to not continue loading a specific url. Instead, launch the proper application (mostly a browser) to handle the url.

Cannot open skype custom URL from android WebView

I'm currently developing an application with android WebView.
However, the website has a link that will invoke Skype application.
The link work fine when try with Chrome browser on android, but it return an error when try to open in android WebView. Actually, it gave me an Webpage not Available error.
Any advice?
Regards.
You should check the url that is click/opened in the webview and check if there is an application that handles this kind of urls:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == skype url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} else {
view.loadUrl(url);
}
return true;
}
});

Url open in full screen instead of Webview

I am working on an Android project, and my task is to open a url in an embedded webview. Here is the code. When a button is clicked I open the url as follows:
yookosBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linearLayout.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
webview.loadUrl("https://www.google.com.pk/");
}
});
1: When i open the google.com it is perfectly opened in embedded webview:
But when I replace the link with "http://videoshare.loveworldapis.com/commentredirect.php" url, the link is opened in full screen instead of embedded portion of webview as shown below:
Can you tell me what modification should I do to open the second website into embedded webview instead of full screen.
The WebView, by default, will open successive URLs by firing an intent, and opening the browser. To disable it so all URLs load in the WebView do this:
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
I suspect your web site load involves an HTTP redirect, and that redirect is causing the browser to open.

Closing HTML viewer from my app

I have a html file which I am opening by launching the HTML viewer. Is there a way to tell HTML viewer from my app to close the page?
Here is the code I am using to launch the html file
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = new File("/data/data/Sample.html");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/html");
startActivity(intent);
Yes i am sure you want to view this HTML file inside your application instead of launching native browser. If this is the case then create WebViewClient and set it inside the WebView.
For example:
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
and set it inside the webview as:
webview.setWebViewClient(new myWebViewClient ());
and then load that URL into the WebView actually. Here is the detailed example.
Do you need to close the web page or transfer the control to other activity..If you go by the second option I would recommend you to use finish(); method at the close method where you use because it takes to the previous activity or you can choose your own destined activity.

Categories

Resources