Tag: icon

Tooltips class for BlackBerry

here is the copy of tooltips class for BlackBerry,
with 2 different approach.

first: [1]
Easy solution to create popup style tooltips on BlackBerry.
Extend TooltipScreen instead of the usual MainScreen, usage:

add(new ButtonField(“myButton”), “My Tooltip text”);
and the source:

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;
        }
    }
}

second: [2]

package ..;

/*

Display a 'Tooltip" (String) at a specified location for s specified time
This Tooltip can also be removed.
Note that in my testing, the display time is not consistent.

Following example shows it being added and removed using focus events.

ButtonField bf = new ButtonField("Test") {
    MyTooltip _tooltip;
    protected void onFocus(int direction) {
        if ( _tooltip != null ) {
            _tooltip.removeToolTip();
            _tooltip = null;
        }
        // Display tooltip at 50,50 for 5 seconds
        _tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), "Press to test", 50, 50, 5);;
    }
    protected void onUnfocus() {
        if ( _tooltip != null ) {
            // We have displayed a Tooltip - remove it
            _tooltip.removeToolTip();
            _tooltip = null;
        }
    }
 };

*/

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

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();
            }
        }

    }

}

[1] http://www.naviina.eu/wp/blackberry/390/
[2] http://supportforums.blackberry.com/t5/Java-Development/Problem-with-Tooltip/td-p/102847


How to use notification icon (ApplicationIndicator)

you can add your own icon at top of blackberry main screen using ApplicationIndicator[1]

ApplicationIndicator class
To add/modify/remove notification icon we can use ApplicationIndicator class:

Represents an application indicator that consists of an icon and optional numeric value.
The icon is placed in indicators area along with others such as new mail counter, calendar reminders, missed calls counter, security status and others.

Application can register only one indicator and has to register it every time the phone starts.
All indicators are cleared upon phone restart.
Indicators may be not visible if there will be no space on statusbar
Indicator dimensions depend on current theme and phone screen size.
The icon size varies from 10×10 up to 24×24.
If indicator is initialized with a big icon then an exception is thrown.
Indicator can have optional numeric value that usually serves as a counter.
The value can be negative, 0 or positive.
If indicator is used as a counter it’s better to hide it when its value is equal to 0.
If indicator value is too big, it will display ’99+’ or ’999+’
Add notification icon
For this we should create instance of ApplicationIcon:

Application icon class is used in conjunction with application indicator and application messages.
It incapsulates image and its painting properties.

EncodedImage mImage = EncodedImage.getEncodedImageResource(“indicator_icon.png”);
ApplicationIcon mIcon = new ApplicationIcon(mImage);
Then we should register ApplicationIndicator with ApplicationIndicatorRegistry:

ApplicationIndicatorRegistry.register(icon, iconOnly, visible)
icon – an icon to be displayed in the indicators area
iconOnly – specifies if indicator should have icon representation only and no value
visible – specifies whether indicator has to be visible initially

ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
ApplicationIndicator indicator = reg.register(mIcon, false, true);
Modify indicator icon and value
To modify icon, use ApplicationIndicator.setIcon method:

ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
ApplicationIndicator appIndicator = reg.getApplicationIndicator();
appIndicator.setIcon(icon);
To modify number value, use ApplicationIndicator.setValue method:

ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
ApplicationIndicator appIndicator = reg.getApplicationIndicator();
appIndicator.setValue(value);
If very big value is provided then indicator will display ’99+’ or ’999+’.
To hide or show indicator, use ApplicationIndicator.setVisible

Unregister ApplicationIndicator
Most likely you don’t want your app indicator to stay in statusbar, it’s better to ApplicationIndicatorRegistry.unregister() it on app close.

[1] http://stackoverflow.com/questions/1465224/blackberry-how-to-use-notification-icon-in-statusbar


blackberry HomeScreen icon

How to get preferred size of HomeScreen icon?

here is the answer[1]:

int height = net.rim.blackberry.api.homescreen.HomeScreen.getPreferredIconHeight();
int width = net.rim.blackberry.api.homescreen.HomeScreen.getPreferredIconWidth();

then update the icon:

net.rim.blackberry.api.homescreen.HomeScreen.updateIcon( icon );

here is the icon size from Ui guidelines[3].

How to create Roll over Icon[2]:

  • for BB OS before 4.7*
public class HomeScreenIcon extends UiApplication
{
    public static void main(String[] args)
    {
        //Check for the argument defined in the project properties.
        if (args != null && args.length > 0 && args[0].equals("gui"))
        {
           HomeScreenIcon theApp = new HomeScreenIcon(false);
            theApp.enterEventDispatcher();
        }
        else
        {
            HomeScreenIcon theApp = new HomeScreenIcon(true);
            theApp.enterEventDispatcher();
        }
    }
    public HomeScreenIcon(boolean autoStart)
    {
        if (autoStart)
        {
        //The application started using the auto start entry point.
        //Setup the rollover icons.
        final Bitmap regIcon = Bitmap.getBitmapResource("1.png");
        final Bitmap icon = Bitmap.getBitmapResource("2.png");

        invokeLater(new Runnable()
        {
            public void run()
            {
                ApplicationManager myApp =
                  ApplicationManager.getApplicationManager();
                boolean keepGoing = true;

                while (keepGoing)
                {
                    //Check if the BlackBerry has completed its
                        startup process.
                    if (myApp.inStartup())
                    {
                        //The BlackBerry is still starting up,
                             sleep for 1 second.
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (Exception ex)
                        {
                          //Couldn't sleep, handle exception.
                        }
                    }
                    else
                    {
                        //The BlackBerry has finished its
                             startup process.
                        //Set the rollover icons.
                        HomeScreen.updateIcon(regIcon, 0);
                        HomeScreen.setRolloverIcon(icon, 0);
                        keepGoing = false;
                    }
                 }
                 //Exit the application.
                 System.exit(0);
            }
        });

    }
    else
    {
         //The application was started by the user.
         //Start the application and display a GUI.
         MainScreen ms = new MainScreen();
         ms.setTitle(new LabelField("Hello there."));
         pushScreen(ms);
    }
  }
}

[1] http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=1484
[2] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800608/How_To_-_Define_a_rollover_icon_for_an_application.html?nodeid=1162799&vernum=0
[3] http://na.blackberry.com/eng/deliverables/6625/Dimensions_for_screens_images_and_icons_476251_11.jsp


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress