The question can be a little vague, I have created a webview and showing the webpage of the URL which I get from some server. Now When i navigate to different pages , On one page there is only a button which will send me a different URL , I need that when user is on that page and click on that button the webview should hide and user continue other operation. I have read that addJavascriptInterface is needed to do this , can anyone tell me if this is possible and how to achieve this , any good tutorial will work.
I also tried this but of no use
wv.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
HitTestResult result = wv.getHitTestResult();
if (result.getType() == HitTestResult.UNKNOWN_TYPE) {
Message msg = mHandler.obtainMessage();
wv.requestFocusNodeHref(msg);
}
return false;
}
});
topupWV.setWebViewClient(new WebViewClient());
topupWV.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("XXXXX")) {
try {
URL urls = new URL(url);
query = urls.getQuery();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
you can do something like this for detecting click within a webview
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isNetworkConnected()) {
//DO YOU OPERATION HERE
}
return true;
}
});
Related
I have questions. and sorry my pool english skill.
In android webivew,
I load some page. and this page is redirect another page.
when redirect, url is contain some data(post, form-data).
I want catch contain data. befor loaded url.
I think, override onPageStarted method and catch data. but cannot.
how can I?
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mWebView.postUrl(URL, postdata.getBytes("UTF-8")); // Sample Page
} catch (UnsupportedEncodingException e) {
Logger.log(TAG, e.getMessage());
}
}
private class WebViewClientClass extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//catch redirect form-data.
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
}
Please add below line then only it will use your custom class.
webView.setWebViewClient(new WebViewClientClass());
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//write your code here, get redirected url in this method and do your operation and then move ahead.
return false;
}
Above method will always called when redirect url is getting loaded.
Hope this will help you.
here my code...am already implemented WebViewClient concept.it works well in web view..In my App have content saring via fb,twitter,g+,,,fb also login and open in webview...but when am click share to fb icon in my app after that all process will work on browser..but i want all process in webview only...
oncreate
webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
if (Build.VERSION.SDK_INT < 8) {
...
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
webView.loadUrl("www.example.com.");
WebViewClient
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onLoadResource (WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
You must override your shouldOverrideUrlLoading() method in WebViewClient class and following code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
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;
}
Go through the documentation for more information.
Use a if clause in your shouldOverrideUrlLoading method that returns false;
Example from Building web apps in web views
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
I'm trying to fetch and display a webpage using WebView, there are chances of page redirection when the webpage is loaded, so is there any option of reading URL's coming as response from first request? I have to perform certain condition checks based on response URL.
Look at shouldOverrideUrlLoading():
WebViewClient wvc = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url) {
if (!url.startsWith("http://m.youtube.com/")) {
// do something
return true;
} else if (url.startsWith("http://m.youtube.com/watch?")) {
// do something different
return true;
}
return false;
}
};
The following steps worked for me:
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
if(url.equals(" abc.com")){
//do some thing
}
else
//do some other thing
super.onPageFinished(view, url);
}
});
In my webview , I get all url in the webViewClient method
public boolean shouldOverrideUrlLoading(WebView view, String url) {}
I want to throw the url to other application if the url is from user clicking. Otherwise webview load the url self.But I can't distinguish them.How should I do?
Thanks for your help!
I follow the method in How can I get onclick event on webview in android? .
there are some alternatives using dispatchTouchEvent Android singleTap/OnClick in WebView
Boolean changedUrl = false;
String currentUrl = null;
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
changedUrl = false;
currentUrl = webView.getUrl();
}
// change either onPageStarted or shouldOverrideUrlLoading
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(changedUrl && !url.equals(currenturl)) {
// page has been clicked
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(changedUrl && !url.equals(currentUrl)) {
// page has been clicked
return true;
}
return false;
}
});
webView.setOnTouchListener( new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.webView && event.getAction() == MotionEvent.ACTION_DOWN){
changedUrl = true;
}
return false;
}
});
I want to have a busy indicator of some sort when pages are loading. My indicator will be full-screen like a splash screen. This is because the android webview blanks the page to white before it starts loading, unlike a normal browser.
A simple solution might be suggested. for example, set loading true in onPageStarted() and set false in onPageFinished().
#Override
public void onPageFinished(WebView view, String url) {
setLoading(false);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setLoading(true);
}
the problem with this is that the website i'm loading results in multiple start / finished cycles for a single click. maybe this is redirects, or iframes. i'm not sure ... but the result is that the "busy" indicator flashes on and off disturbingly.
Considering that, another ideas is to keep a count of pages loading- incrementing in onPageStarted() and decrementing in onPageFinished(). something like this,
private int loadingCount = 0;
#Override
public void onPageFinished(WebView view, String url) {
loadingCount--;
if (loadingCount == 0) {
setLoading(false);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (loadingCount == 0) {
setLoading(true);
}
loadingCount++;
}
I'm running into this problem: page started / page finished are not matched. I'm not sure why exactly, but I think it might have to do w/ redirects.
Any ideas?
Try this:
private boolean loadingFinished = true, redirect = false;
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.setVisibility(View.VISIBLE);
view.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setTitle(url);
loadingFinished = false;
progressDialog = new ProgressDialog(FacebookWebView.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(" Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
//finish();
} else {
redirect = false;
}
}
I'm not exactly sure, but this should solve your problem with redirects. In the case of several frames on the same page, probably counting in onPageFinished till the number of frames, and then dismiss "busy dialog", should be a solution.