[JAVA] [Android] Hook the tap of the anchor link in WebViewClient

Hook anchor link taps in WebViewClient

I think that it is not a rare case, but it is a memorandum when you want to hook the tap of the anchor link (link in the page) on the web screen with Android WebView and make something work on the application side at that timing.

** Example: If you include WebView in ScrollView, the anchor link will not work, so I want to hook the tap of the anchor link on the application side to scroll the ScrollView **

When you want to scroll the WebView and the native View together, you may implement something like the above

Addictive: WebViewClient shouldOverrideUrlLoading is not called by tapping an in-page link

At first, what should I do when I see the Url in WebViewClient's shouldOverrideUrlLoading and it is an in-page link! I was thinking, but shouldOverrideUrlLoading is not called for links within the same page ...

Solution: Called if WebViewClient is onPageFinished

ShouldOverrideUrlLoading is not called when tapping an in-page link, but onPageFinished is. You can also take a Url as an argument, so what should I do here? For example, suppose the in-page link looks like this:

<a href="#top">Back to top</a> 

It is OK if you write as follows using WebViewClient

 webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if(url.contains("#top")) {
                    /**Write what you want to do here**/
                }
            }
        });

that's all.

Recommended Posts

[Android] Hook the tap of the anchor link in WebViewClient
About the basics of Android development
Order of processing in the program
What wasn't fair use in the diversion of Java APIs on Android
Android development, how to check null in the value of JSON object
Get the result of POST in Java
Understand the basics of Android Audio Record
The identity of params [: id] in rails
The story of AppClip support in Anyca
The story of writing Java in Emacs
Write the movement of Rakefile in the runbook
Summarize the life cycle of Java objects to be aware of in Android development
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[Order method] Set the order of data in Rails
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the idea of anonymous classes in Java
3 ways to import the library in Android Studio
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
The story of tuning android apps with libGDX
Get your version number in the Android app
Import files of the same hierarchy in Java
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
[Rails] I want to display the link destination of link_to in a separate tab