The Hyperlink is a component that can trigger a custom action on a specially highlighted text. There are two types of Hyperlinks, the ones that can be triggered by tapping anywhere on the text of their containers and the ones where the developer defines certain words or sentences as the linked text.

Figure 1. Hyperlinks can cover the entire text of their containers (left) or can be triggered from certain words (right)

Converting a String into a HyperLink, requires that the developer defines a text and a URL as follows:

HyperLink hyperLink = new HyperLink("This is a hyperlink", "http://www.google.com");

Converting a word within a String into a HyperLink requires that the hyperlinked text is wrapped around a <link> tag as shown below:

HyperLink hyperLink = new HyperLink("Please click <link=http://www.google.com>here</link> to login");

By default, when Hyperlinks are tapped, they launch the browser to the selected URL and the MIDlet is paused. As soon as the user presses the hardware Back key, the browser is closed and the MIDlet is resumed.

Developers can override this default behavior and define their own actions when Hyperlinks are tapped. This is done by calling the method setDefaultPlatformRequest(false). A HyperlinkListener should be attached to the Hyperlink instance, so that the custom action can be coded inside the call back method notifyHyperLinkListener as shown here:

HyperLink hyperLink = new HyperLink("This is a hyperlink", "http://www.google.com");
hyperLink.setHyperLinkListener(new HyperLinkListener() {

    public void notifyHyperLinkListener(HyperLink hyperlink, String url) {
         *  The url from the hyperlink instance has been tapped.
         *  Implement here a custom action

Last updated 17 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.