By calling my own 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 control its appearance. I managed to create my own menu by creating a class that extends PopupScreen and has my MenuItem as configured LabelField with the abstract invokeAction() method. I made the invokeAction() method abstract to emulate the run () method of MenuItem .

enter image description here

Everything was fine, but I remember something. What if my boss asks me to implement a native MenuItem like Switch Application and Close . I don’t think that the Close implementation will be a problem, but Switch Application and another native MenuItem like Show Keyboard will give me a problem. So I come up with a different 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 take an instance of MainScreen as my parameter to get the MenuItem lists and add it to the existing CustomMenu . The invokeAction() method is an analogue of the run() MenuItem method. And this is the result of what I did:

enter image description here

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

+6
source share
2 answers

Update

After receiving more clarification from the OP, I think the answer is correct that you cannot do what they ask ... to create your own menu, but programmatically call MenuItem commands / actions from the built-in menu .


Original answer

If you understood correctly that you want to create your own menu, but you do not want to use the built-in BlackBerry menu, because your menu should look different?

If so, then I would suggest a different approach. I think BlackBerry wants you to do this, it is usually to add MenuItem objects to the built-in menu , but then change the various menu properties in your Screen class makeMenu() method:

 protected void makeMenu(Menu menu, int context) 

Here are the BlackBerry docs about it , and here is an example that combines adding the menu items that you show above with some changes to the appearance of the menu. I hope you find that this is an easier way to do what you want, which is not related to the need to associate MenuItems with the built-in menu, but 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 need to support OS 6.0 and above, you have other options (let us know). In addition, it is possible that your motivation for writing your own menu is that you want to change the font color, which I don’t think you can do with the code that I show above. Again, if this part of your design, please let us know. Thanks.

+1
source

All Articles