Android create a toast with link in webview [duplicate] - java

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"

Related

Get link of the clicked image in a webview

I want to get the link of an image clicked inside a Webview to display it in an new intent. The site that i want the image from is not opening the images in fullscreen when clicked.
I want something like the code down bellow but the website doesn't open images in the same tab.
Reference Code:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with it
}else{
view.loadUrl(url);
}
}
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever
image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with
it
Intent intent = new Intent(this, NewActivity.class);
//pass from here the data to next activity and load there
intent.putExtra("yourURL", imageUrl);
startActivity(intent)
}else{
view.loadUrl(url);
}
}
}

How to get Url of the WebView using WebResourceRequest in Java?

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.

How to check if Link is offline or broken with Android Webview?

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());
}
});

How to open a link in a browser that was clicked in a WebView in Android [duplicate]

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;
}
}

Nothing happens when I click a link inside my webview

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

Categories