Open main menu

CDOT Wiki β

Changes

4.b. Add Image to the Menu
==== 4.b. Add Image to the Menu ====
4.b.1. Run Add static fields for menu item number:<br/><source lang="java">final static int MENU_ADD = 0;  final static int MENU_EDIT = 1; final static int MENU_DELETE = 2; final static int MENU_EMAIL = 3; final static int MENU_MAP = 4;</source> 4.b.2. Create an abstract class that extends MenuItem to be used for creating items with image:<br/><source lang="java"> /** * Concrete implementation of abstract class MenuItem */ class ImageMenuItem extends MenuItem { /** * Creates a new MenuDemoMenuItem object */ ImageMenuItem(String title, int ordinal, int property, int itemNum) { super(new StringProvider(title), ordinal, property);  // Create Image object from project resource Bitmap bitmap; switch(itemNum) { case 0: bitmap = Bitmap.getBitmapResource("add.png"); break; case 1: bitmap = Bitmap.getBitmapResource("edit.png"); break; case 2: bitmap = Bitmap.getBitmapResource("delete.png"); break; case 3: bitmap = Bitmap.getBitmapResource("email.png"); break; case 4: bitmap = Bitmap.getBitmapResource("map.png"); break; default: bitmap = Bitmap.getBitmapResource("none.png"); break; } Image image = ImageFactory.createImage(bitmap);  // Set image as this menu item's icon setIcon(image); } } </source> 4.b.3. Change the instantiation of menu items to use the applicationnew class created ''ImageMenuItem''. Click on Pass the name of the menu you should see item to the followingconstructor:<br/><source lang="java"> // MenuItem addItem = new MenuItem(new StringProvider("Add Student"), 200, 2); // without image ImageMenuItem addItem = new ImageMenuItem("Add Student", 100, 1, MENU_ADD);</source>[[Image: BB_MenuBB_MenuImage.png | 300px]]<br/>
1
edit