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

2

6

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?

Creekmore answered 25/3, 2013 at 9:41 Comment(1)
What minimum OS version do you have to support?Nostradamus
N
1

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.

Nostradamus answered 6/4, 2013 at 23:11 Comment(2)
Hi Sir. Actually my main concern in creating my own Menu is because of its highlighter. I don't think there's a way to change the highlighter of the menu by using the Menu Class of the Blackberry. That's why I implemented my own Menu by using a VerticalFieldManager and a PopupScreen.Creekmore
@JjTuibeo, Ok, I understand. Unfortunately, I don't know of a way to do that. Obviously, you know how to create your own menu with a PopupScreen, but I don't know how you could link the Close, Switch Application, etc. items to the default menu items of the same name. I also tried doing something similar on OS 6.0, by triggering the Command associated with the default menu items. Unfortunately, that yields the same IllegalStateException that you get by trying to call the run() method :(Nostradamus
A
1

These links may help you

Create CUSTOM MENU in Blackberry(Blogspot)

and

Create CUSTOM MENU in Blackberry(WordPress)

Actinic answered 6/4, 2013 at 10:12 Comment(0)
N
1

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.

Nostradamus answered 6/4, 2013 at 23:11 Comment(2)
Hi Sir. Actually my main concern in creating my own Menu is because of its highlighter. I don't think there's a way to change the highlighter of the menu by using the Menu Class of the Blackberry. That's why I implemented my own Menu by using a VerticalFieldManager and a PopupScreen.Creekmore
@JjTuibeo, Ok, I understand. Unfortunately, I don't know of a way to do that. Obviously, you know how to create your own menu with a PopupScreen, but I don't know how you could link the Close, Switch Application, etc. items to the default menu items of the same name. I also tried doing something similar on OS 6.0, by triggering the Command associated with the default menu items. Unfortunately, that yields the same IllegalStateException that you get by trying to call the run() method :(Nostradamus

© 2022 - 2024 — McMap. All rights reserved.