I am building an android application. I am showing external webpage in webview. I have followed these steps:
Load external website in webview. For example example.com, it loads fine in webview
There is an option in example.com site to launch Dialer app on button click. Here is the code.
<div class="center">
<input type="image" src="btn.png" onclick="location.href='tel:0000';"/>
</div>
When I go to example.com from mobile browser and click on button, it can launch Dialer app with phone number
When I click from webview it shows this error
Web page not available
The web page at tel:0000 could not be loaded because:
net::ERR_UNKNOWN_URL_SCHEME
I do not know what is went wrong. Any clue will be helpfull.
NB: I am using real phone number (here it is 0000).
Thank you
You should set a WebViewClient to the WebView and than override shouldOverrideUrlLoading method as follow:
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, request.getUrl());
view.getContext().startActivity(intent);
}
return super.shouldOverrideUrlLoading(view, request);
}
});
Related
I am developing an android app with a web app loading on WebView. I want to invoke the web app button actions. I have implemented some changes on webpage to invoke native methods as shown below.
mWebview.addJavascriptInterface(new Object() {
#JavascriptInterface // For API 17+
public void callNativeHome() {
Log.d("btnsetup", "btnsetup");
}
}, "btnsetup");
But now I can't make any changes on web end as it's not my web screen. How I can access the button click events by ID or class name?
Web access is mandatory to Bind JavaScript code to Android code.
Android:
val webView: WebView = findViewById(R.id.webview)
webView.addJavascriptInterface(WebAppInterface(this), "Android")
Web:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
You can simply follow Bind JavaScript code to Android code.
Another workaround is to find the navigation URL. You can check/compare the redirection URL called after clicking that button.
For more information, you can check Handle page navigation in webview.
I want to display content from remote URL in android web view, which will be return by java script tag. In my case , java script tag return image , which should be load into web view.
Here is code to render image/content from remote URL.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<script src='http://mytestdomain.com/test.php?w=300&h=250' type='text/javascript'></script>" ;
webView.loadData(customHtml, "text/html", "UTF-8");
}
URL load the content but image are broken and do not display properly. Though i can click on on the image and land into correct link.
I have proper permission
<uses-permission android:name="android.permission.INTERNET"/>
How can i fix it. Any help is appreciate.
I think the issue is with your js code. I tried this code and image is showing fine.
<html>
<body>
<h3>A demonstration of how to access an IMG element</h3>
<img id="myImg" src="http://via.placeholder.com/350x150" alt="The Pulpit Rock" width="304" height="228">
<p>Click the button to get the URL of the image.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myImg").src;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
Also enable DomStorage:
webView.getSettings().setDomStorageEnabled(true);
Broken image issue because presence of protocol-relative url (//) for the image returned by our server. These kind of URLs (//:mytestdomain.com) will force the protocol to be the same as it's parent page, and the app's web view might be missing a base scheme for the image to inherit. I placed proper protocol into url and it working fine now
I am facing a strange issue.
In my application, I need to load a static html file based on clicked button in a WebView from assets folder.
Now among 5 html files, one html file contains 15 static links. These links need to redirect user to the mentioned url in a mobile browser. I have used target="_blank" for that purpose as follows in my html file.
<div class="lid">Railway Reservation </div>
Now, this works fine in a sample application with a simple WebView without any WebViewClient added to it.
But I need a WebViewClient for my other functionalities. So at that time target="_blank" is totally ignored. And the url is opened in a WebView itself.
I found a workaround, that I can use shouldOverrideUrlLoading as follows:
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if(url.equalsIgnoreCase("https://www.irctc.co.in/"))
{
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else
return super.shouldOverrideUrlLoading(view, url);
}
});
So this is opening that particular url in a default browser.
So basically my questions are:
Why is target="_blank" ignored while we use WebViewClient? And is there any other workaround for this issue? Because I have 15 links, which I would need to compare. I can't load all urls in a new browser, as there are a few links, which need to be opened in a same WebView, too.
I have an idea for a workaround. Replace all links which should been opened in a new window with a custom schema. Then you can handle that by your own.
For a minimal interruption set also a custom theme and handle all configuration changes:
<activity android:name="com.example.LinkHandler"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="your.link.handler.schema"/>
</intent-filter>
</activity>
Then in your link handler you can read the url by using getIntent().getData().
Please also keep in mind that you should handle http and https, I skipped that in my short example above.
Here is an JavaScript example how to rewrite the urls:
for(var i=0; i<document.links.length; i++) {
if(document.links[i].target == "_blank") {
document.links[i].href = "your.schema.for."+document.links[i].href;
}
}
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 4</li>
</ul>
I have loaded a website with my WebView. But when I click in any link of that website , the control has been transferred from my WebView to the Android built in browser. I want to browse that site with my WebView.
I want to download book from my site. Another task that is very important to me is when I will download a book from my site ,it will give a password to my software ( android part ). What do I do to receive a password from an web site from the WebView.
This question is very important for me to complete my project.
Waiting for your replies...
The code for webView is just as following:
wv = (WebView) findViewById(R.id.mainwebview);
WebSettings ws = wv.getSettings();
ws.setBuiltInZoomControls(true);
wv.loadUrl("http://m.feedbooks.com/store");
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl(url);
above code can help you to display website with your webview
Have a look at the WebViewClient1
You can override shouldOverrideUrlLoading(WebView view, String url)
Iam looking for playing media files inside webview Can you please help me out ..what iam missing..I loaded webview as : _webview.loadUrl("file:///android_asset/3gp.html"); and the code inside my html file is as:
<body>
<object width="550" height="400">
<param name="movie" value="sample.3gp">
<embed src="file:///android_asset/sample.3gp" width="550" height="400">
</embed>
</object>
</body>.
Iam not getting the output. Please hellp me out ..Thanks in advance
To play SWF files you have to enable plugins:
_webview.getSettings().setPluginState(PluginState.ON);
and if your .html file uses javascript:
_webview.getSettings().setJavaScriptEnabled(true);
EDIT to answer comment
In your situation you need to use your own webview at the moment links are redirecting to the browser:
_webview.setWebViewClient(new DownloadWebViewClient());
public class DownloadWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false; // Allows your webview to handle clicked links
}
}