Create a Send menu

In this task, an edit field is created that accepts text that a BlackBerry device user types. This text is stored in a context object, and this context object is used to retrieve a list of Send menu commands whose associated applications can accept the text. A Send menu is created with these Send menu commands.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.sendmenu.*;
    
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.XYEdges;
    import net.rim.device.api.ui.component.EditField;
    import net.rim.device.api.ui.component.Menu;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.decor.BorderFactory;
    
    import org.json.me.JSONException;
    import org.json.me.JSONObject;
  2. In the screen class, declare instance variables to represent an edit field and the text that the user types in this field.
    private EditField _textToSend;
    private String _textString;
  3. In the screen constructor, create a new EditField object to represent the field that the user types in.
    _textToSend = new EditField("Send: ", "Type the text to send here", 255,
                                Field.USE_ALL_WIDTH);
  4. To specify the appearance of the EditField object, invoke setBorder(), setPadding(), and setMargin(). In this task, a bevel border is created for the EditField object, and a padding area and margin area are added.
    _textToSend.setBorder(BorderFactory.createBevelBorder(new XYEdges(3, 3, 3, 3)));
    _textToSend.setPadding(8, 8, 8, 8);
    _textToSend.setMargin(15, 15, 15, 15);
  5. Add the EditField object to the screen.
    add(_textToSend);
  6. Override makeMenu() of the MainScreen class.
    protected void makeMenu(Menu menu, int instance)
    {
  7. Invoke super.makeMenu() to add the default menu items for BlackBerry device applications to the menu.
        super.makeMenu(menu, instance);
  8. Invoke getText() of the EditField class to retrieve the text that the user typed in the edit field.
        _textString = _textToSend.getText();
  9. Create a new JSONObject object to represent the context information for the Send menu.
        JSONObject context = new JSONObject();
  10. In a try/catch block, invoke put() of the JSONObject object to populate the context information using key-value pairs. Use the constants that are defined in the SendCommandContextKeys class as keys. Catch a JSONException, which indicates that the specified key is null.
        try
        {
            context.put(SendCommandContextKeys.TEXT, _textString); 
            context.put(SendCommandContextKeys.SUBJECT, "Your Text");
        }
        catch (JSONException e)
        {
            System.out.println(e.toString());
        }
  11. Create an array of SendCommand objects to represent the Send menu commands that you want to include in the Send menu. Invoke SendCommandRepository.getInstance() to retrieve the singleton instance of the SendCommandRepository. Invoke get() and specify as parameters the type of content to send, the context information for the content, and a boolean value that indicates whether all of the commands should be retrieved regardless of whether their associated applications can be opened.
        SendCommand[] sendCommands = SendCommandRepository.getInstance().get(
                               SendCommand.TYPE_TEXT, context, false);
  12. Determine whether the array of SendCommand objects is not null and contains commands, and if so, create a new SendCommandMenu object to represent these commands and add it to the menu.
        if (sendCommands != null && sendCommands.length > 0)
        {
            menu.add(new SendCommandMenu(sendCommands, 0, 0));
        }
    }

Code sample: Creating a Send menu

import net.rim.blackberry.api.sendmenu.*;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BorderFactory;

import org.json.me.JSONException;
import org.json.me.JSONObject;

public class SendMenuAPIDemo extends UiApplication
{
    public static void main(String[] args)
    {
        SendMenuAPIDemo theApp = new SendMenuAPIDemo();
        theApp.enterEventDispatcher();
    }

    public SendMenuAPIDemo()
    {
        pushScreen(new SendCommandDemoScreen());
    }
}

class SendCommandDemoScreen extends MainScreen
{
    private EditField _textToSend;
    private String _textString;

    public SendCommandDemoScreen()
    {
        setTitle("Send Menu API Demo");

        _textToSend = new EditField("Send: ", "Type the text to send here",
                                    255, Field.USE_ALL_WIDTH);
        _textToSend.setBorder(BorderFactory.createBevelBorder(
                                            new XYEdges(3, 3, 3, 3)));
        _textToSend.setPadding(8, 8, 8, 8);
        _textToSend.setMargin(15, 15, 15, 15);

        add(_textToSend);
    }

    protected void makeMenu(Menu menu, int instance)
    {
        super.makeMenu(menu, instance);

        _textString = _textToSend.getText();

        JSONObject context = new JSONObject();
        try
        {
            context.put(SendCommandContextKeys.TEXT, _textString); 
            context.put(SendCommandContextKeys.SUBJECT, "Your Text");
        }
        catch (JSONException e)
        {
            System.out.println(e.toString());
        }

        SendCommand[] sendCommands = SendCommandRepository.getInstance().get(
                                               SendCommand.TYPE_TEXT, context, false);
        if (sendCommands != null && sendCommands.length > 0)
        {
            menu.add(new SendCommandMenu(sendCommands, 0, 0));
        }
    }
}
Previous topic: Creating a Send menu

Was this information helpful? Send us your comments.