How do we open an activity after clicking some specific link or button of a website which is from some website and open using webview?
Use shouldOverrideUrlLoading to give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.
For more information shouldOverrideUrlLoading
Try this it will help you:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("url")) {
Intent intent = new Intent(contxt, YourActivity.class);
startActivity(intent);
return true; // Handle By application itself
} else {
view.loadUrl(url);
return true;
}
}
}
Related
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())
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.
I've found an app's source code and it opens web pages using external browsers, like Chrome or others. I want to make it access those pages from inside the app, without using an external browser. I think that I should change the code below somehow, but I don't know how.
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dmc.teiion.gr")) {
//open url contents in webview
return false;
} else {
//here open external links in external browser or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
yourWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Or change
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
to
view.loadUrl(url);
I have a webview to show a webpage. In that webpage button click event i'm redirecting to another page. From there another button click event im redirection to 3rd page. Instead of redirecting to 3rd page i need to show my next activity.
I'm using the following code but my first page itself not loading
#Override
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://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
}
return true;
}
});
webview.loadUrl("http://Default URL to load first time");
}
Please tell me how to preceed it?
Try returning false when you are not handle the event. This is from the WebClient reference.
True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
Your code should be like this.
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
return true;
}
return false;
}
});
Try this variant:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://My 3rd page redirecting URL"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
} else {
webview.loadUrl(url);
}
return true;
}
Try onLoadResource. This should work for webview. Sometimes onOverride function doesnt work especially when you are calling an intent.
I am using webviewclient to open the html page. The html page is having a anchor tag.
when i click on the anchor tag my phone dialer activity should be launched.
when i click on this anchor tag in external browser (android default browser ), it is launching the phone dialer, but as i am using the webviewclient (browser with in my application). i am unable to launch the phone dialer.
is there any way to achieve this using webviewclient ?
You should override this method
public boolean shouldOverrideUrlLoading(WebView wv, String url)
{
if(isNumber)
{
//Get the number from the URL
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:12345"));
startActivity(intent);
return true;
}
return false;
}
in the WebViewClient, and return ture that means you want to handle this by yourself instead of the webView.
The document is here.
I think is the best way to do this. So Please try this, I know i am too late to reply this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( URLUtil.isNetworkUrl(url) ) {
view.loadUrl(url);
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true; /*
return false;*/
}