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
Related
I'm have an application which uses a login and the webview in my homepage. Whenever i want to logout from the webview the session is just terminated in the webview but i need it to come back to the local login page. Or should i need to write some JavaScript code for the logout in the WebviewClient or should i just need to override the logout URL?
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
swipeRefreshLayout.setRefreshing(false);
}
if (url.equals("abc.com") == true) {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String username = preferences.getString(KEY_USERNAME, "");
if (username == null) {
return;
}
}
}
And Whenever i hit the back button in the webview page, the page just reloads but does not come out of the application.
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());
}
});
As a step in auto-filling some fields in a flow I need to scrape a URL that has its interesting content filled dynamically by JavaScript.
Based on a cars license plates I can look up information on a government website, set an option and a value and submit.
The resulting page will hold the desired content in different tabs so I'll also have to navigate those. (Sorry cant give direct link to this but selecting "Registreringsnummer", using "YN28579" as the value and pressing "Søg" will take you to the page.)
I've done this with a WebViewClient in another Activity so it is possible to navigate the website directly on the android phone/tablet. But in this other Activity I don't want to display the resulting page just scrape some of the data items.
Here is how I've done it with the WebViewClient:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
((Button)findViewById(R.id.btnBack)).setVisibility(View.VISIBLE);
wv = (WebView) findViewById(R.id.wv);
reg = getIntent().getStringExtra("reg");
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
private ProgressDialog pd;
private int count = 0;
#Override
public void onPageFinished(WebView view, String url) {
if (count==1) {
view.loadUrl("javascript:document.getElementById('regnr').checked=true;"
+"document.getElementById('soegeord').value='"+reg+"';"
+"document.getElementById('searchForm').submit();"
+"DMR.WaitForLoad.on();");
} else if (count>=2) {
view.loadUrl("javascript:document.body.innerHTML " +
"= '<div class=\"tabNav\">'+document.getElementsByClassName('tabNav')[0].innerHTML+'</div>';" +
"document.getElementsByClassName('h-tab-content')[0].style.width='320px';" +
"document.getElementsByClassName('h-tab-btns')[0].style.width='320px';" +
"document.getElementsByClassName('h-tab-btns')[0].style.height='45px';" +
"document.getElementsByTagName('ul')[0].style.display='inline';" +
"document.head.appendChild='<meta name=\"viewport\" content=\"width=device-width\">';" +
"document.body.style.minWidth ='300px';");
if (pd!=null) {
pd.dismiss();
}
view.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (pd==null || !pd.isShowing()) {
pd = ProgressDialog.show(SkatActivity.this, "cars.dk", "Vent venligst...", true, false);
}
count++;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("https://motorregister.skat.dk/dmr-front/appmanager/skat/dmr?_nfpb=true&_nfpb=true&_pageLabel=vis_koeretoej_side&_nfls=false");
}
I can't seem to figure out how to incorporate this in
Do you set the WebView as invisible? Found this question that seems to be working along those same lines, but I can't figure out how to get the working WebViewClient hidden but usable inside this other activity?