Wednesday, January 12, 2011

Adding Menu Items in existing Blackberry Apps

Hello every one,

Sometimes it happens that you want to add your own menu item to the existing blackberry applications.When user clicks menu key in an existing application then a menu item added by you is visible say "Open with myApp" or "import with myApp".

This can be achieved using the ApplicationMenuItemRepository class.
net.rim.blackberry.api.menuitem package, enables you to add menu items to BlackBerry applications.

Features:
1) It enables you to add or remove application menu items.
2) It provides constants that define the contexts in which a menu items can appear.

For example:
The ApplicationMenuItemRepository.MENUITEM_CALENDAR constant specifies that the menu item appears when the calander is open.

In this sample we will invoke calander application by clicking on a button and we can see a new menu item added on the calendar app.

Here you go:

package com.Test;

import java.util.Date;
import javax.microedition.pim.Event;
import net.rim.blackberry.api.invoke.CalendarArguments;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class ApplicationMenuDemo extends MainScreen
{
    LabelField lbl;
    ApplicationMenuDemo()
    {
        super();
       
        MyMenuItem myMenuitem = new MyMenuItem(0);
        ApplicationMenuItemRepository.getInstance().addMenuItem(
        ApplicationMenuItemRepository.MENUITEM_CALENDAR, myMenuitem);
       
        setTitle("ApplicationMenuItemRepository Demo");
       
        lbl = new LabelField("imported start date will be displayed here");
       
        ButtonField btn = new ButtonField("import from Calendar");
        btn.setChangeListener(listener);
       
        add(new LabelField("",LabelField.NON_FOCUSABLE));
        add(lbl);
        add(new LabelField("",LabelField.NON_FOCUSABLE));
        add(btn);
    }
   
    FieldChangeListener listener = new FieldChangeListener()
    {
        public void fieldChanged(Field field, int context)
        {
          Invoke.invokeApplication(Invoke.APP_TYPE_CALENDAR, new
                 CalendarArguments(CalendarArguments.ARG_VIEW_DEFAULT));
        }
    };
   
     class MyMenuItem extends ApplicationMenuItem
        {
            String text;
            MyMenuItem(int order)
            {
                super(order);
            }

            public Object run(Object context)
            {
                if (context instanceof Event)
                {
                    Event event = (Event) context;
                   
                    Date date = new Date((event.getDate(Event.START, 0)));
                                       
                    lbl.setText(date.toString());
                }
                return context;
            }

            public String toString()
            {
                return "Import Calander Date";
            }
        }
}
See how it looks:

 

 




link for api details:

http://www.blackberry.com/developers/docs/4.0.2api/index.html
Enjoy coding :)

9 comments:

  1. hello sir,
    i want to ask if i can add a menu item that show in all the bb screens
    and this item can launch my app
    thank you.

    ReplyDelete
    Replies
    1. to point you in the right direction use
      MENUITEM_SYSTEM
      instead of
      MENUITEM_CALENDAR like the above example

      Delete
  2. Hello swati ma'am,
    I am just 1 week old on blackberry apps development environment and want to know if there are some good books available for apps development.
    Thanks and Regards
    SweetCynaiDe

    ReplyDelete
  3. I remember few books:

    1) Beginning Blackberry Development by Apress.
    2) You can also download pdf docs from BB Developer site for various topics like GPS etc...

    ReplyDelete
  4. Thanks Swati ma'am.. i'm following the same..:-)

    ReplyDelete
  5. i am getting duplicate "Import Calander Date " mean more than 1 menu item in menu. How to stop that ?


    thanks

    ReplyDelete
  6. Thank u for your inspiration...
    Successfull for you

    ReplyDelete