I have these codes:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//want url of the webview in here
}
I just want to know, how can I obtain the Url of the webView to override the Url loading. The shouldOverrideUrlLoading(WebView view, String url) method is depreceated in API21.
Thanks in advance!
in \Android\sdk\sources\android-25\android\webkit\WebViewClient.java, you can see this code.
So short answer to your question is:
request.getUrl().toString()
Check sources on GrepCode.
boolean shouldOverrideUrlLoading (WebView view, String url) deprecated in API 24, boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) added in API level 24
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(Uri.parse(request.getUrl().toString()).getHost().endsWith("google.com")) {
//The host application wants to leave the current WebView and handle the url itself.
return true;
}
if(Uri.parse(request.getUrl().toString()).getHost().endsWith("yandex.com")) {
//The current WebView handles the url.
return false;
}
if(Uri.parse(request.getUrl().toString()).getScheme().equals("file")) {
//The current WebView handles the url.
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
view.getContext().startActivity(intent);
return true;
}
private String cUrl = "";
cUrl = webView.getUrl();
Then you can use it wherever you want.
Related
I want to stop loading the url in same webview when clicking any item within the webpage.
for this I have tried shouldoverrideurlloading() but its not catching the url so not able to override it.
our application is single page application that may be reason due to which shouldOverrideUrlLoading() is not getting called.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(view,uri);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(view, uri);
}
private boolean handleUri(WebView view,final Uri uri){
if (uri.toString().contains("messages") || uri.toString().contains("proddetail") || uri.toString().contains("about-us")) {
Intent intent = new Intent(webActivity.this, nativeActivity.class);
startActivity(intent);
return true;
}
return true;
}
In this its not catching url containing messages..
Another method I have tried is catching url using doUpdateVisitedHistory() and using view.stoploading() for stopping the loading of url within same webview.
#Override
public void doUpdateVisitedHistory(WebView view, String url,boolean isReload) {
if ( url.contains("messages"))
{
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.stopLoading();
//opening new fragment
SharedFunctions.getInstance().replaceFragment(CompanyPage.this, new CompanyPage(uri.toString().replace("www","m")), "Messages", getActivity().getSupportFragmentManager(), true, true);
}
}, 1000);
}
Using this the loading of url is not stopping as when coming back to previous fragment that loading has been done in webview.
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 question already has answers here:
WebView link click open default browser
(6 answers)
Closed 9 years ago.
I have a webview that loads in a fragment in Android. When the user taps a link on the webpage loaded in the webview, I want the new page to be loaded in the default browser on Android outside of my app. Any ideas how to do this? Thanks!
What I have so far:
FrameLayout layout = new FrameLayout(context);
WebView webView = new WebView(getActivity());
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadUrl(InvestBetterURL);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return false;
}
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon)
{
view.setVisibility(View.GONE);
}
#Override
public void onPageFinished (WebView view, String url)
{
view.setVisibility(View.VISIBLE);
}
});
layout.addView(webView);
Change the shoudlOverrideUrlLoading to true. Check the link:
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
UPDATED:
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("google.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
}
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