creating customized tooltip with image in blackberry java

131 Views Asked by At

I need to have a tooltip (as shown in below fig) at bottom of my blackberry screen ,tooltip consists with two buttons with image as background for each button .and this tooltip is visible/invisible to user only when menu button is pressed.and by pressing this buttons in tooltip user should be directed to other screen.

sir i tried below code but clicking on button over the tooltip it is not navigating to secondscreen.what mistake i have done.

Myscreen

 public final class MyScreen extends MainScreen
{
public boolean onMenu(int instance)
{
     int action = Toolbar.push();
     if(action == Toolbar.ACTION_BUTTON1)
     {
      Dialog.alert(" " );
        // UiApplication.getUiApplication().pushScreen(new 
 SecondScreen());
     }

     return false;
}
public MyScreen()
{        
    setTitle("MyTitle");
}
}    

secondscreen

 public class SecondScreen extends MainScreen {
public SecondScreen()
{

setTitle("listview");

 }
 }
1

There are 1 best solutions below

10
Kevin On

I have found that when you need a custom clickable field in Blackberry, such as a bitmap with text, it is easier to create a single field that handles the click events. This means extending a field, and overriding the layout, and paint methods. Trying to do the same thing with nested managers and fields usually gives issues when it comes to navigating with the trackpad or listening to touch events (because a manager is not focusable unless it has a focusable child).

In a previous post I made, I gave examples of how to create button fields based on an example from the Blackberry Developer site. You'll need BaseButton and ImageSubtitleButton from my post.

Once you have those you should be able to add a status bar to your screen.

HorizontalFieldManager toolbar = new HorizontalFieldManager(USE_ALL_WIDTH);
ImageSubtitleButton button1 = new ImageSubtitleButton("test1", "test.png");
toolbar.add(button1);

ImageSubtitleButton button2 = new ImageSubtitleButton("test2", "test.png");
toolbar.add(button2);

setStatus(toolbar);

Please note that referencing image resources like this isn't ideal, and you should make a resource manager with constants, as opposed to using inline image names.

Update: Since you want this toolbar to replace your native bb menu, you'll have to override the onMenu method which is triggered when the menu button is pressed. Because you also want the menu to disappear when clicking away from the menu, you are going to have to make a new Screen, with a transparent Background, with toolbar as status.

Toolbar.java

public class Toolbar extends MainScreen
{   
     public static final int ACTION_NONE = 0;
     public static final int ACTION_BUTTON1 = 1;
     public static final int ACTION_BUTTON2 = 2;

     private int action = ACTION_NONE;
     private HorizontalFieldManager container;

     private Toolbar()
     {  
          setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0)); // Make Screen transparent.
          getMainManager().setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0)); // Make manager transparent.

          container = new HorizontalFieldManager(USE_ALL_WIDTH);
          ImageSubtitleButton button1 = new ImageSubtitleButton("test1", "test.png")
          {
               public void clickButton()
               {
                    // Set the selected action, and close
                    action = ACTION_BUTTON1;
                    close();
               }
          };
          container.add(button1);

           ImageSubtitleButton button2 = new ImageSubtitleButton("test2", "test.png")
          {
               public void clickButton()
               {
                    // Set the selected action, and close
                    action = ACTION_BUTTON2;
                    close();
               }
          };
          container.add(button2);

          setStatus(container);
     }


     protected boolean touchEvent(TouchEvent message)
     {
         // Listen for touch events, if they are above the toolbar, close screen.
         if (message.getEvent() == TouchEvent.DOWN)
         {
             int y = message.getY(1);
             if (y < getHeight() - container.getHeight())
             {
                  close();
                  return true;
             }
         }

           return super.touchEvent(message);
     }

     public boolean onMenu(int instance)
     {
          // When menu button is pressed again, close
          close();
          return false;
     }

     protected boolean keyChar(char c, int status, int time)
     {
           // When back button is pressed, close
           if (c == Characters.ESCAPE)
           {
                 close();
                 return true;
           }
           return super.keyChar(c, status, time);
     }

     public static int push()
     {
          final Toolbar toolbar = new Toolbar();
          UiApplication.getUiApplication().invokeAndWait(new Runnable()
          {
                public void run()
                {
                     UiApplication.getUiApplication().pushModalScreen(toolbar);
                }
           });

           // Return selected action, or ACTION_NONE, if user closed menu
           return toolbar.action;
     }
}

Now when you want to use this toolbar on your screen, override the onMenu of the screen like this

public boolean onMenu(int instance)
{
     int action = Toolbar.push();
     if(action == Toolbar.ACTION_BUTTON1)
     {
           // Handle this button press
     }

     return false;
}