2

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("<a href=\"#_ftn0\">[0]</a>", "text/html", "UTF-8");

if(OnClickedUrl == "#_ftn0")
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
ATES
  • 281
  • 1
  • 11
  • 29

3 Answers3

1

you should override the url loading like below:

wv.loadData("<a href=\"http://www.example.com/#_ftn0\">[0]</a>", "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;
   }
}
MinFu
  • 353
  • 1
  • 13
  • make url the **urlyouclicked** is the full path url – MinFu Feb 29 '16 at 01:55
  • pretty odd, no reaction when i clicked link? I have tired no if blog too. – ATES Feb 29 '16 at 02:02
  • try call toast message below before if condition in shouldovrrideurloading **Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show();** – MinFu Feb 29 '16 at 02:26
  • you should see the full path url you clicked then use the url shown in the if condition – MinFu Feb 29 '16 at 02:27
  • `public boolean shouldOverrideUrlLoading(WebView view, String url){ Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show(); return true; }` **no reaction :S** – ATES Feb 29 '16 at 02:33
  • the problem was your anchor tag href to the same page "#_ftn0" the webview won't reload the page it will only scroll to the div with this "_ftn0" id. shouldOverrideUrlLoading will only call when the is a loading of a new url – MinFu Feb 29 '16 at 02:44
  • maybe you can to change your anchor href url to a full path url like this **** – MinFu Feb 29 '16 at 02:51
  • i changed "#_ftn0" to "ftn0" and have tried shouldOverrideUrlLoading() wtih if statement and without if statement, no result. I have tried onLoadResource() it work but do not know it in this work. – ATES Feb 29 '16 at 02:53
  • This is true: `[0]` This is wrog: `[0]` all error is this :) thanks MinFu – ATES Feb 29 '16 at 03:31
0

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
        }
    }
});
jackson95
  • 136
  • 10
  • I have tired `shouldOverrideUrlLoading` but when i click `[0]` url, can not get no reaction. – ATES Feb 29 '16 at 01:52
  • The problem is not url because i am trying code without if block and no work. – ATES Feb 29 '16 at 02:08
  • Try creating toast within `onLoadResource`. – jackson95 Feb 29 '16 at 02:15
  • When i traying onLoadResource getting toast message but how to get specify if(url.equals(url)) block in onLoadResource, because returning resource url? – ATES Feb 29 '16 at 02:22
  • Edited the code, try if this works. No values to return since it is a void. – jackson95 Feb 29 '16 at 02:26
  • @لَاإِلٰهَإِلَّاالله Sorry didn't quite get you in "_I have tried onLoadResource() it work but do not know it in this work_", did you mean that you do not know what value to return in both methods? For `shouldOverrideUrlLoading` return true to override loading and then go to `onLoadResource`, else return false to do nothing. The toast can then be created under `onLoadResource` instead. – jackson95 Feb 29 '16 at 03:24
0

Besides override shouldOverrideUrlLoading the url should match this format:"://",or it will not work. So you can modify "#_ftn0" to "example://#_ftn0"

wngxao
  • 72
  • 5