Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
grantfox
Posts: 4
Registered: ‎05-23-2009
My Device: Not Specified
Accepted Solution

How to display a hyperlink?

Hi all,

 

I'm trying to include a hyperlink on a MainScreen which has standard functionality (e.g. brings up standard menu with 'Get Link' etc).

 

I can use ActiveRichTextField("h t t p : // www . myurl . com") which gives me the required functionality but displays the URL whereas I want to be able to display "My URL Link" to the user instead.

 

If anyone can give me any advice on this then I'd be very grateful.  Also, if anyone has acheived this and could post some example code for how to do it then that would be very much appreciated.

 

I'm using JDE 4.3.0 (but could probably upgrade to 4.5.0 if that helps the situation).

 

Many thanks,

Grant.

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: How to display a hyperlink?

Extend net.rim.device.api.ui.component.LabelField class

 

Add additional field to the class, let say

 

private String httpLink;

 

modify class constructor to take two parameters - label and httpLink.

 

declare menu item "Get link"  to get the link and invoke browser with value of private String httpLink

 

after that override method

 

public ContextMenu getContextMenu()

 

and add "Get link" menu item to the context menu that returned by this method.

 

That's it.

 

You can use this class instance on your screen.

 

It will display label specified in constructor, but will have "Get link" context menu and will open a webpage by the link you specified in constructor.

 

 

New Developer
grantfox
Posts: 4
Registered: ‎05-23-2009
My Device: Not Specified

Re: How to display a hyperlink?

Many thanks for that. I was rather hoping that it would be possible to utilise the standard built-in functionality which is provided for opening hyperlinks, but I guess that's not possible.

 

So, I have done as suggested and created a HyperlinkField as shown in my code below.

 

This works fine and gives the "Get Link" context menu option when you press the menu button. However, when you click on the trackball you just get a menu with "Copy" and "Full Menu".  Ideally I'd like the "Get Link" option to be included then as well. Do you know how I can acheive this?

 

 

import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.ContextMenu; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Font; import net.rim.device.api.ui.MenuItem; import net.rim.blackberry.api.browser.Browser; /** * A custom LabelField to display a hyperlink */ public class HyperlinkField extends LabelField { private String mUrl; private MenuItem mGetLinkMenuItem; /** * Constructs a new HyperlinkField instance with provided label, style and URL * @param label Label string to be displayed. * @param style Field style for the label. * @param url URL to be opened in the browser. */ public HyperlinkField(String label, long style, String url) { super(label, style | FOCUSABLE); Font font = this.getFont().derive(Font.UNDERLINED); setFont(font); mUrl = url; mGetLinkMenuItem = new MenuItem("Get Link", 0, 0) { public void run() { // invoke browser with URL Browser.getDefaultSession().displayPage(mUrl); } }; } /** * Constructs a new HyperlinkField instance with provided label and url with default style * @param label Label string to be displayed. * @param url URL to be opened in the browser. */ public HyperlinkField(String label, String url) { this(label, 0, url); } public ContextMenu getContextMenu() { // Add our "Get Link" menu item to the context menu ContextMenu menu = super.getContextMenu(); menu.addItem(mGetLinkMenuItem); return menu; } }

Many thanks,

Grant

 

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: How to display a hyperlink?

[ Edited ]

In your custom class also override the method:

 

protected boolean navigationClick(int status, int time)

 

instantiate a Menu class instance, let say menu2Show

 

add necessary menu items 

 

and call:

 

menu2Show.show();

 

to consume the trackball click event - return true.

 

In case you want to pass trackball event to the parent object (screen/manager) return false.

Message Edited by tbilisoft on 24-05-2009 03:42 PM
New Developer
grantfox
Posts: 4
Registered: ‎05-23-2009
My Device: Not Specified

Re: How to display a hyperlink?

Thanks again.  I did as suggested, however, found that I could only have the menu displayed in the top-right corner, not bottom-centre.

 

I've found a work-around as follows:

 

Added a getGetLinkMenuItem() method to my HyperlinkField class to return my "Get Link" menu item.

 

Overriden the makeMenu() method in my Screen as follows:

 

 

protected void makeMenu(Menu menu, int instance) { super.makeMenu(menu, instance); if ( (instance == Menu.INSTANCE_CONTEXT) && (getFieldWithFocus() == mHyperlinkField) ) { menu.add(mHyperlinkField.getGetLinkMenuItem()); } }

It's a shame that the functionality that I want isn't all contained within my HyperlinkField class but I can live with that.

 

Thanks again for your help.

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: How to display a hyperlink?

You are welcome :smileyhappy:
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: How to display a hyperlink?

It depends what you expect to do with this. It would probably be more helpful to put your link into

html and use a browse field fed with an httpconnection subclass that contains the text locally ( although you

can read local files on later OS's, this approach works through out ). This gives you the flexibility of html

etc.

 

For the right application, the stringpattern is nice but if you are designing your thing, going with

a structured document may be better. (html for example). If you need to parse unstructured things,

string pattern or other ad hoc code is about all you can do 

 

New Developer
grantfox
Posts: 4
Registered: ‎05-23-2009
My Device: Not Specified

Re: How to display a hyperlink?

I'd be interested to see some example code for how to acheive this using a StringPattern as it seems fairly complicated (or maybe I'm just being dumb!). Thanks.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: How to display a hyperlink?

I can't remember exactly what you want to do but if you have a word in a document you want to map to a link,

the word can be used to build an arbitrary menu item. Someone posted an example earlier. If you get to 

author the documents, you'd probably be better off with html and a browser field or you could make some

other kind of xml I suppose.

New Developer
redbutt
Posts: 37
Registered: ‎03-02-2009
My Device: Bold 9700

Re: How to display a hyperlink?

The code below will cause "My Pattern" to appear as a link in any text field (not HTML however).  The menu that is triggered will have your "Get Link" menu item as the first item in the menu.

 

-----

 

AppMenuItems.PatternMatchMenuItem patternMatchMenuItem = new AppMenuItems.PatternMatchMenuItem(0);
ApplicationDescriptor appdesc    = ApplicationDescriptor.currentApplicationDescriptor();
ApplicationMenuItem[] patternMatchMenuArray = new ApplicationMenuItem[1];

 

patternMatchMenuArray[0] = patternMatchMenuItem;
                
// Remove all previously registered patterns for safety                
PatternRepository.removePatterns(appdesc);

 

// Add default patterns to the Pattern Repository.
PatternRepository.addPattern( appdesc,"My Pattern", PATTERN_TYPE_EXACT_MATCH, patternMatchMenuArray );

 

 

public final class AppMenuItems {

    public final static class PatternMatchMenuItem extends ApplicationMenuItem {
       
        public PatternMatchMenuItem(int order) {
            super(order);
        }
       
        public Object run(Object context) {
            if(context instanceof String) {
                // Do something here

           }
            return context;
        }
       
        public String toString() {
            return "Get Link";
        }
    }

}