Overriding Hyperlink Behavior in Android
This guide addresses a common query of how to override hyperlink behavior in Android, especially for long-press actions. It’ll outline how to customize both click and long-press interactions with hyperlinks.
Analysis
While overriding the click behavior of hyperlinks is straightforward, the long-press action presents an additional challenge:
-
The SDK provides a default long-press menu for hyperlinks, offering Copy and Open as options.
-
This menu appears independent of the
onPageClick
callback, which only handles single clicks. -
As a result, the Open action from this menu bypasses any custom click handling you may have implemented.
Solution
We offer a long-press listener, setOnDocumentLongPressListener
, and it should work the same as with the single click.
-
You can override click behavior with the following code:
requirePdfFragment().setOnDocumentLongPressListener { document, pageIndex, event, pagePosition, longPressedAnnotation -> longPressedAnnotation is LinkAnnotation }
Summary
By implementing both click and long-press listeners, you can create a consistent and customized experience for hyperlink interactions in your PSPDFKit-powered Android application. This approach gives you full control over how hyperlinks behave, whether they’re tapped or long-pressed.
Remember to test your implementation thoroughly to ensure it meets your specific requirements and provides a smooth user experience.
If you encounter any issues or have further questions about customizing hyperlink behavior, don’t hesitate to reach out to our Support team.