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
samieaji
Posts: 6
Registered: ‎08-29-2012
My Device: Bold 9900
Accepted Solution

Tooltip for Button

Good day,

 

I am new to blackberry development. I created a bitmapbuttonfield using the advanced ui examples. It is a button with an image. I want to display a tooltip for the button when the button receives focus. Can anyone please help

 

Thanks in advance

Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Tooltip for Button

Such a mechanism is better implemented on the Manager/Screen level - the tooltip is going to be displayed outside of your button. You will have to override the paint of the Manager to paint everything and then paint the tooltip on top, depending on which field has focus. You'll have to invalidate the Manager on focus changes.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
New Developer
samieaji
Posts: 6
Registered: ‎08-29-2012
My Device: Bold 9900

Re: Tooltip for Button

Pls can you provide a sample of how that is done, I'm kinda new here

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Tooltip for Button

I'm going to put my neck out here and say that creating a tooltip is not something I would expect someone who was new to BlackBerry to be able to write. 

 

To do it properly, I think you have to understand the BlackBerry framework and the event handling, as well as being about to paint your own canvas using low level Graphics operations, and start and stop Timer operations.  This is not the sort of thing that I would expect someone new to BlackBerry to be able to implement.  And I have never seen a full sample. 

 

But this question has been asked before and there are more Threads discussing this on the forum, for example:

http://supportforums.blackberry.com/t5/Java-Development/How-to-display-tooltip/m-p/193744 

 

Have a search round these and see if you find anything of interest. 

Developer
bb2010
Posts: 157
Registered: ‎03-29-2010
My Device: 8300

Re: Tooltip for Button

Create LabelField either On the Top/Bottom and Center of the Bitmap Field.

 

And Extend the BitmapField Class and send the String that has set when Bitmap is Focused and Empty text when Bitmap is Unfocused and in the CustomBitmapfield class overide the below methods

 

protected void onFocus(int direction)
{
 lbl.setText(name);
invalidate();
}

protected void onUnfocus()
{
lbl.setText("");
invalidate();
}

Developer
BrajeshSanodiya
Posts: 134
Registered: ‎08-03-2011
My Device: torch, Z10

Re: Tooltip for Button

[ Edited ]

Hi, 

 

Use this code:

 

 

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;

public final class MyScreen extends TooltipScreen
{

	ButtonField btn1,btn2,btn3;
	public MyScreen() {

		btn1=new ButtonField();
		btn1.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				Dialog.alert("Button 1 Click");
			}
		});
		btn2=new ButtonField();
		btn2.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				Dialog.alert("Button 2 Click");
			}
		});

		btn3=new ButtonField();
		btn3.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				Dialog.alert("Button 3 Click");
			}
		});
		add(btn1, "Button 1");
		add(btn2, "Button 2");
		add(btn3, "Button 3");

	}

}

 

package mypackage;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;

public class TooltipScreen extends MainScreen {

    TooltipScreen screen = this;
    boolean doRedraw = false;//prevent infinte redrawing
    Vector tooltips = new Vector();//vector to hold tooltip strings
    private Timer tooltipTimer = new Timer();
    private TimerTask tooltipTask;
    boolean alive = false;//is the tooltip alive? used to pop it after our timeout
    int count = 0;//used to calculate time tooltip is displayed
    //tooltip popup colours:
    int backgroundColour = 0xeeeeee;
    int borderColour = 0xaaaaaa;
    int fontColour = 0x666666;
    //the tooltip:
    String tooltip;
    int tooltipWidth;
    int yCoord;
    int xCoord;
    //region parameters:
    XYRect contentArea;
    int contentBottom;
    int contentRight;

    public TooltipScreen() {
        super();

        //when timeout reaches 100ms*20 ie. 2seconds set alive to false and redraw screen:
        tooltipTask = new TimerTask() {

            public void run() {
                if (alive) {
                    count++;
                    if (count == 20) {
                        alive = false;
                        invalidate();
                    }
                }
            }
        };

        tooltipTimer.scheduleAtFixedRate(tooltipTask, 100, 100);

    }

    //override add method adds an empty string to tooltip vector:
    public void add(Field field) {
        tooltips.addElement("");
        super.add(field);
    }

    //custom add method for fields with tooltip: add(myField, "myTooltip");
    public void add(Field field, String tooltip) {
        super.add(field);
        tooltips.addElement(tooltip);
    }

    public void setColours(int backgroundColour, int borderColour, int fontColour) {
        this.backgroundColour = backgroundColour;
        this.borderColour = borderColour;
        this.fontColour = fontColour;
    }

    //reset everything when user changes focus,
    //possibly needs logic to check field has actually changed (for listfields, objectchoicefields etc etc)
    protected boolean navigationMovement(int dx, int dy, int status, int time) {
        count = 0;
        alive = true;
        doRedraw = true;
        return super.navigationMovement(dx, dy, status, time);
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        if (alive) {
            Field focusField = getFieldWithFocus();
            tooltip = (String) tooltips.elementAt(screen.getFieldWithFocusIndex());

            //don't do anything outside the norm unless this field has a tooltip:
            if (!tooltip.equals("")) {
                //get the field content region, this may fall inside the field actual region/coordinates:
                contentArea = focusField.getContentRect();
                contentBottom = contentArea.y + contentArea.height;
                contentRight = contentArea.x + contentArea.width;

                //+4 to accomodate 2 pixel padding on either side:
                tooltipWidth = graphics.getFont().getAdvance(tooltip) + 4;

                yCoord = contentBottom - focusField.getManager().getVerticalScroll();
                //check the tooltip is being drawn fully inside the screen height:
                if (yCoord > (getHeight() - 30)) {
                    yCoord = getHeight() - 30;
                }

                //check the tooltip doesn't get drawn off the right side of the screen:
                if (contentRight + tooltipWidth < getWidth()) {
                    xCoord = contentRight;
                } else {
                    xCoord = getWidth() - tooltipWidth;
                }

                //draw the tooltip
                graphics.setColor(backgroundColour);
                graphics.fillRect(xCoord, yCoord, tooltipWidth, 30);
                graphics.setColor(borderColour);
                graphics.drawRect(xCoord, yCoord, tooltipWidth, 30);
                graphics.setColor(fontColour);
                graphics.drawText(tooltip, xCoord + 2, yCoord);
            }
        }
        //doRedraw logic prevents infinite loop
        if (doRedraw) {
            //System.out.println("redrawing screen: " + System.currentTimeMillis());
            screen.invalidate();
            doRedraw = false;
        }
    }
}

 

package mypackage;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

class MyTooltip extends PopupScreen{
    int _x;
    int _y;
    TooltipThread _tooltipThread;

    private MyTooltip(Manager manager) {
        super(manager);
     }
    public void sublayout(int width, int height)    {
        super.sublayout(width,height);
        setPosition(_x,_y);
        System.out.println("Tooltip x: " + Integer.toString(_x) + ", y: " + Integer.toString(_y));
    }
    protected void applyTheme() {
        // Overriden to suppress Border etc.
    }
    public void removeToolTip() {
        if ( _tooltipThread != null ) {
            _tooltipThread.dismiss();
        }
    }
    private void display(UiApplication uiApp, int x, int y, int displayTime) {
        _x = x;
        _y = y;
        _tooltipThread = new TooltipThread(uiApp, this, displayTime);
        _tooltipThread.start();
    }

    public static MyTooltip addToolTip(UiApplication uiApp, String toolTipString, int x, int y, int displayTime) {
        VerticalFieldManager manager = new VerticalFieldManager(Manager.FIELD_VCENTER|Manager.NON_FOCUSABLE) {
            protected void paint(Graphics graphics) {   
                graphics.setColor(0x00FFFFFF); // White
                graphics.fillRect(0,0,getWidth(),getHeight());
                graphics.setColor(0x00000000); // Black
                graphics.drawRect(0,0,getWidth(),getHeight());
                super.paint(graphics);
            }
        };
        MyTooltip toolTip = new MyTooltip(manager);
        LabelField label = new LabelField(' ' + toolTipString + ' ', LabelField.NON_FOCUSABLE);
        label.setFont(Font.getDefault().derive(Font.PLAIN, 16));
        toolTip.add(label);
        toolTip.display(uiApp, x, y, displayTime);
        return toolTip;
    }

    class TooltipThread extends Thread {

        Object _notifyObject = new Object(); // Used to allow user to dismiss this Tooltip
        PopupScreen _tooltip; // Screen we are going to display
        UiApplication _ourApplication; // access to pushGlobalScreen and dismissStatus from our Application
        int _displayTime; // in seconds

        public TooltipThread(UiApplication ourApplication, PopupScreen tooltip, int displayTime) {
            _tooltip = tooltip;
            _ourApplication = ourApplication;
            _displayTime = displayTime;
        }

        public void run() {
            _ourApplication.pushGlobalScreen(_tooltip, 999, false);
            synchronized(_notifyObject) {
                try {
                    _notifyObject.wait(_displayTime * 1000);
                } catch (Exception e) {
                }
            };
            _ourApplication.dismissStatus(_tooltip);
        }

        public void dismiss() {
            // notify the waiting object to stop the Thread waiting
            synchronized(_notifyObject) {
                _notifyObject.notify();
            }
        }

    }

}

 

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Tooltip for Button

tooltips as global screens, and each using it's own thread, well, whatever floats your boat i guess...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Tooltip for Button

I agree with Simon, I have a number of issues with the code in general, but the general point is that PopupScreens on top of the actual screen are an overhead and have a number of side-effects that make them not well suited as tooltips.  If you review the Thread I pointed you at previously you will see similar comments.  

 

They are a simpler way of implementing the same visual effect, but the cost for me is too high. 

 

But the real point I would make is that I no longer use tooltips, unless told to.  I have implemented tooltips using PopupScreens, then reimplemented them as a Screen overwrite and in the end, decided that they really did not add anything to the application, certainly nothing that justified the additional coding.  The time spent developing Tooltips is better spent on the User Experience design.  For me, Tooltips in most cases, just add "bling" that really only helps the first time user and isn't needed if the screen is understandable. 

 

Well that is my opinion.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Tooltip for Button

we have also used the screen overwrite approach, and have an extension of mainscreen that we use instead of it, so the work cost to implement them in a new application is very low.
in some cases i like tooltips, but in our latest application we tried to make the icons understandable without them, so it's more of a design question if you want to use them.

If you want to use them, don't use popupscreens.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Tooltip for Button

Here is one example from the Internet:

http://v4ks1n.wordpress.com/2011/01/28/tooltips-class-for-blackberry/

It is very limited - everything needs to be added directly to the TooltipScreen - but provides a very good starting point.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!