Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoking native MenuItem(Switch Application, Close, etc.) in my CustomMenu in blackberry

I need to create a custom menu in my blackberry application so that I can manage its appearance. I managed to create my custom menu by creating a class which extends a PopupScreen and having my MenuItem as a customized LabelField with abstract invokeAction() method. I made the invokeAction() method as abstract to emulate the run() method of MenuItem.

enter image description here

Everything was ok but I remember something. What if my boss ask me to implement native MenuItems like Switch Application and Close. I don't think implementing Close will be a problem, but the Switch Application and other native MenuItem like Show Keyboard, this will give me a problem. So I come up for another solution and this is my code:

public CustomMenu(MainScreen screen) {
        super(vfm);
        Menu menu = screen.getMenu(0);
        for(int i = 0; i < menu.getSize(); i++){
            final MenuItem finalMenu = menu.getItem(i);
            vfm.add(new CustomMenuItem(finalMenu.toString(), Field.FOCUSABLE){
                protected boolean invokeAction(int action) {
                    finalMenu.run();
                    return true;
                }
            });
        }
    }

This is the constructor of my CustomMenu. I accept an instance of MainScreen as my parameter to get the the lists of MenuItem and add it to my existing CustomMenu. The invokeAction() overridden method there is the counterpart of run() method of MenuItem. And this is the result of what I did:

enter image description here

I managed to put those native MenuItem in my CustomMenu but the problem is when I invoke(click) those native MenuItem(Switch Application, Close) I got an IllegalStateException. Is there a way to get the implementation of those native MenuItem? Or a way to capture the run() method of MenuItem then invoke it in my CustomMenu?

like image 540
Jj Tuibeo Avatar asked Mar 25 '13 09:03

Jj Tuibeo


2 Answers

Update

After getting more clarification from the OP, I believe the correct answer is that you cannot do what they're asking ... to create your own menu, and programmatically invoke MenuItem commands/actions from the built-in menu.


Original Answer

If I understand you correctly, what you want is to create your own menu, but you don't want to use the built-in BlackBerry menu, because your menu needs to look different?

If that is the case, then I would suggest another approach. I think the way BlackBerry wants you to do this is to add your MenuItem objects to the built-in Menu normally, but then change various properties of the menu in your Screen class's makeMenu() method:

protected void makeMenu(Menu menu, int context)

Here are the BlackBerry docs on doing this, and here is an example that combines adding the menu items you show above, with some changes to the Menu appearance. Hopefully, you find this an easier way to do what you want, that doesn't involve having to link MenuItems from the built-in Menu, to yours:

public class MenuScreen extends MainScreen {
   private Background _menuBackground;
   private Border _menuBorder;
   private Font _menuFont;
   private MenuItem _customMenuItems[];

   public MenuScreen() {
      setTitle("Custom Menu Sample");
      getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));

      RichTextField f = new RichTextField("Creating a custom menu") {                   

         protected void paint(Graphics g) {
            int oldColor = g.getColor();
            g.setColor(Color.WHITE);
            super.paint(g);
            g.setColor(oldColor);
         }
      };
      add(f);

      // Customize the look (border/color/font) of the BB menu here:
      XYEdges thickPadding = new XYEdges(10, 10, 10, 10);
      _menuBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_DOTTED);        
      _menuBackground = BackgroundFactory.createSolidTransparentBackground(Color.DARKMAGENTA, 80);

      try
      {
         FontFamily family = FontFamily.forName("BBCasual");
         _menuFont = family.getFont(Font.PLAIN, 30, Ui.UNITS_px);
      }
      catch (final ClassNotFoundException cnfe)
      {
         UiApplication.getUiApplication().invokeLater(new Runnable()
         {
            public void run()
            {
               Dialog.alert("FontFamily.forName() threw " + cnfe.toString());
            }
         });              
      }       

      // Add our own menu items, too
      _customMenuItems = new MenuItem[3];
      _customMenuItems[0] = new MenuItem("Hola Dora!", 110, 10) {
         public void run() {
            Dialog.inform("Hola Dora!");
         }
      };
      _customMenuItems[1] = new MenuItem("Close popup!", 111, 10) {
         public void run() {
            Dialog.inform("Close popup!");
         }
      };
      _customMenuItems[2] = new MenuItem("Hola Diego!", 112, 10) {
         public void run() {
            Dialog.inform("Hola Diego!");
         }
      };

      addMenuItem(_customMenuItems[0]);
      addMenuItem(_customMenuItems[1]);
      addMenuItem(_customMenuItems[2]);      
   }

   protected void makeMenu(Menu menu, int context)
   {
      menu.setBorder(_menuBorder);
      menu.setBackground(_menuBackground);
      menu.setFont(_menuFont);
      // invoking super.makeMenu() will add {Close, Switch Application, etc.} items
      super.makeMenu(menu, context);
   }
}

Results

enter image description here

Note: if you only have to support OS 6.0 and above, you have some other options, too (let us know). Also, it's possible that your motivation for writing your own menu is because you want to change the font color, which I don't think you can do with the code I show above. Again, if that's part of your design, please let us know. Thanks.

like image 122
Nate Avatar answered Nov 04 '22 06:11

Nate


These links may help you

Create CUSTOM MENU in Blackberry(Blogspot)

and

Create CUSTOM MENU in Blackberry(WordPress)

like image 23
alishaik786 Avatar answered Nov 04 '22 05:11

alishaik786