I'm currently creating a small Android App including a WebView. If there is an error (e.g. net::Err_NAME_NOT_RESOLVED) the WebView should load a custom html file. I pasted the html file under assets/www/mypage.html
My code:
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webview.loadUrl("file:///android_asset/www/mypage.html");
Toast.makeText(getActivity().getBaseContext(), "Just FMI", Toast.LENGTH_SHORT).show();
}
});
What Am I doing wrong? The Toast is working fine.
Kind regards!
Try change code like below
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
view.loadUrl("file:///android_asset/www/mypage.html");
Toast.makeText(getActivity().getBaseContext(), "Just FMI", Toast.LENGTH_SHORT).show();
}
});
Related
I am unable to play Twitch Streams in my Android application.
Twitch is only providing their embed url for playing Streams.
I am using this url: https://player.twitch.tv/?channel=CHANNEL_NAME
I am using a WebView to load this url by
mWebview.loadUrl(url);
But this method has several issues, like the video tends to autoplay but gets stuck initially. I need to press Pause first and then the Play button to start this stream.
Try using this code.
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl(url);
setContentView(mWebview );
}
I'm trying to create custom error message for my app which is using webview. so far i'm using below code which has a very minimal problem... when i click links on the webview the page is reloading the same page instead of processing the clicked link. my links on the webview are download link so either it should download using the same app or open down-loader options
protected void fetch_web(){
final WebView web;
web = (WebView) findViewById(R.id.voice_mail_view_port);
NoNetwork = (Button) findViewById(R.id.btn_no_internet);
NoNetwork.setVisibility(View.GONE);
String mdb = "http://192.168.23.1/default.php";
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
final Activity MyActivity = this;
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Fetching feeds...");
MyActivity.setProgress(progress * 100);
loader();
if(progress == 100) {
MyActivity.setTitle(R.string.app_name);
deLoader();;
}
}
});
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl((mdb));
}
protected void loader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.VISIBLE);
}
protected void deLoader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.INVISIBLE);
}
Sample download link http://192.168.23.1/download.php?id=1
Can someone help me out, i suspect my error is coming from here
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
Try forcing the app to open links in other browsers
Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
If it fails let me know to help more.
This question already has an answer here:
Android WebView - Intercept clicks
(1 answer)
Closed 6 years ago.
How to view toast when i clicked an url in webview without go to url, only display a toast message?
wv = (WebView) findViewById(R.id.wV);
wv.loadData("[0]", "text/html", "UTF-8");
if(OnClickedUrl == "#_ftn0")
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
you should override the url loading like below:
wv.loadData("[0]", "text/html", "UTF-8");
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show();
if(url.equal(urlyouclicked)){//here you compare the url you click
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
}
return true;
}
}
You can create a shouldOverrideUrlLoading1 event (refer to link 1 for API reference) and place your code within the method. However you will need to specify the url(s) where the loading should be overridden. If you want to override for all urls just remove the if statement.
wv.setWebViewClient( new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Make sure the url to override loading is the correct one
if (url.contains("www.xxx.com")) {
return true;
}
return false;
}
#Override
public void onLoadResource(WebView view, String url){
//Make sure the url to override loading is the correct one
if (url.contains("www.xxx.com")) {
//create toast message here
}
}
});
Besides override shouldOverrideUrlLoading the url should match this format:"://",or it will not work. So you can modify "#_ftn0" to "example://#_ftn0"
I use an Android Webview to load a URL like this:
mWebView = (WebView)v.findViewById(R.id.webView);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("someUrl");
How can I detect if the link the webView should load is offline or broken?
Yes you can do detect broken links in WebView using onRecicedError() in WebViewClient Class.
You have to create an object for WebViewClient and implement onRecicedError() method and set WebViewClient Object using setWebViewClient() method in WebView
Eg:
webView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(errorCode == 404){
Log.d("Webview", "Invalid URL: "+url);
}
else if(errorCode == 500){
Log.d("Webview", "Internal Server error: "+url);
}
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
This is a part of my android app, i have created a webview...but when i click on any link inside the webview...nothing happens....here is my code...i want to launch the link either in any browser or any installed download manager...i m a newbie..please help me out with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
// no need to use title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://vesit-d7b.host22.com/Assignments/ECCF.html");
}
Please use this code and check that your url is being caught by the webview or not.
myweb.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
see what url is coming when you click the link.....
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v("here","the link is ::" + url);
view.loadUrl(url);
return true;
}
and then copy it on browser to see whether it is a valid link or not..
#Ritesh remove the shouldOverrideUrlLoading method it will work