My objective is to set auto detection of phone number and links inside a WebView, and if user doing a long press on number phone for example, a dialog appear to confirm the call
This can be done by using following lines of coed.
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG,"shouldOverrideUrlLoading url: "+url);
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
mcContext.startActivity( intent );
return true;
}
Related
I Have a php login form working perfectly on mobile browser but in my webview when the user press on the submit button the page refresh.
What do you mean with "the page refresh" ? There is a blank page or nothing happens ?
You can intercept and proceed deep link ( e.g: a link in your webview ) like this :
myWebView.setWebViewClient( new WebViewClient()
{
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView inView, String inUrl)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( inUrl ) );
inView.getContext().startActivity(intent);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading( WebView inView, WebResourceRequest inRequest )
{
Intent intent = new Intent(Intent.ACTION_VIEW, inRequest.getUrl());
inView.getContext().startActivity(intent);
return true;
}
} );
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;*/
}
I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.
I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.
Here is the code that does not work:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if (urlObj.getHost().equals("192.168.1.34")) {
view.loadUrl(url);
return true;
} else {
view.loadUrl(url);
return false;
}
} catch (Exception e) {
}
}
}
In both cases ( return true and return false) the URL is handled by my application.
Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.
Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
//Allow the WebView in your application to do its thing
return false;
} else {
//Pass it to the system, doesn't match your domain
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
//Tell the WebView you took care of it.
return true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.
Hope that helps!
If you can't be bothered to explain what "does not work properly" means, we can't be bothered to give you much specific help.
Use shouldOverrideUrlLoading(). Examine the supplied URL. If it is one you want to keep in the WebView, call loadUrl() on the WebView with the URL and return true. Otherwise, return false and let Android handle it normally.
Add the following to your activity
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("192.168.1.34")) {
view.loadUrl(url);
Log.d("URL => ", url); // load URL in webview
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent); // Pass it to the system, doesn't match your domain
return true;
}
I've created a web view app, the page that is displayed features market:// links but upon clicking them I get the 404 screen along with the error that the protocol is not supported. I've tried looking through documentation but was unable to find anything relating to this. Any help is much appreciated.
For me the JavaScript thing wasn't a solution as the HTML is not under my control. So if you need to control this from the application side, then there is a relative simple solution: Derive from WebViewClientand inject the implementation using WebView.setWebViewClient(). All you need to override in your WebViewClientimplementation is the shouldOverrideUrlLoading method as shown here:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("market://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
For me this works fine.
HOPE THIS HELPS YOU
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
else{
view.loadUrl(url);
return true;
}
}
For the links to work you have to have the market app installed on your device/emulator.
Also your app need to request a permission to access network.
UPD:
as a workaround you can call java code from within the webview, for example if you generate links like this:
..
Define a javascript function named go():
<script type="text/javascript">
function go(link) {
if (handler) {
handler.go(link);
} else {
document.location = link;
}
}
</script>
You then can pass in a handler object into the WebView:
webview.addJavascriptInterface(new Handler() {
#Override
public void go(String marketUrl) {
//start market intent here
}
}, "handler");
Handler interface can be defined as follows:
public interface Handler{
public void go(String url);
}
Work for me:
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://myweb.com");
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
It is important to understand how the webview and its clients (webviewclient and webchromeclient) works. Please go through the http://therockncoder.blogspot.in/2014/04/understanding-androids-webchromeclient.html
In the webviewclient's shouldOverrideUrlLoading() method, you can decide if you want to open the link in new browser or within the webview. If you don't override this method, it will by default open the link in a new browser outside of the your android application.
If you want to open within webview, override the method as below
public boolean shouldOverrideUrlLoading(WebView view, String url) { <br>
Log.v("activity", "INSIDE WEBVIEW CLIENT ON shouldOverrideUrlLoading");
view.loadUrl(url);
return false; //need to understand return value based on usage
}
Schemes like whatsapp://send?text=Hello%20World! or market://details?id=xx.xx.xx will open the corresponding apps automatically if they are opened outside the webview and if that app is installed on the handset.
If you want to open certain links within webview and specific schemes outside webview, you need to override WebChromeClients onCreateWindow() method as explained in the link provided above. It should solve the purpose.
Instead of adding check for particular scheme, Modifying #sven solution, this will work for all schemes
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host= Uri.parse(url).getHost();
if (host == null) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
view.loadUrl(url);
return false;
}
Simplest solution
Intent newApp = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(newApp);