blackberryfruitpie

In the Blackberry development tutorial series we are going to improve upon the previous tutorial by adding Blackberry menu items and a click event handler. Blackberry menus are good for a number of things but we find using them instead of presenting all of the options on the screen provides a much cleaner user experience. We find that if the user experience is good, the user is ultimately going to come back to the application as they enjoyed it. Jump past the break to see how to implement click event handlers and Blackberry Menu items.

 

The code below is from the previous example as we are just building on the user experience in this tutorial so we do not have to go about implementing a new project. This code should also look familiar to you or the previous tutorial on Blackberry Development 101 would be the best place to start at.  Check out the code below to see what we have done to improve the interface.

 

// MyScreen.java

package com.binarywasteland.contaclientinformation;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{ 
// Set the displayed title of the screen 
setTitle("Client Information");

BasicEditField _firstName = new BasicEditField("First Name:", "");
BasicEditField _middleName = new BasicEditField("Middle Name:", "");
BasicEditField _lastName = new BasicEditField("Last Name:", "");
BasicEditField _address1 = new BasicEditField("Address 1:", "");
BasicEditField _address2 = new BasicEditField("Address 2:", "");
BasicEditField _city = new BasicEditField("City:", "");
BasicEditField _provstate = new BasicEditField("Province/State:", "");
BasicEditField _zippostalcode = new BasicEditField("Postal/Zip Code:", "");
BasicEditField _country = new BasicEditField("Country:", "");
BasicEditField _phone = new BasicEditField("Phone:", "");
BasicEditField _email = new BasicEditField("Email:", "");

Below we are looking at the Button object that has been created to handle an event when the button is clicked. CONSUME_CLICK indictaes that the button will consume the click event. NEVER_DIRTY will protect the button from being checked to see if it has been modified and can save you some problems down the road by including this. FIELD_HCENTER & FIELD_VCENTER are used to center the button in the middle of the screen and looks a lot cleaner than a button hanging on the left side of the screen.

ButtonField _submit = new ButtonField("Submit", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | Field.FIELD_HCENTER | Field.FIELD_VCENTER);

VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(_firstName);
vfm.add(new SeparatorField());
vfm.add(_middleName);
vfm.add(new SeparatorField());
vfm.add(_lastName);
vfm.add(new SeparatorField());
vfm.add(_address1);
vfm.add(new SeparatorField());
vfm.add(_address2);
vfm.add(new SeparatorField());
vfm.add(_city);
vfm.add(new SeparatorField());
vfm.add(_provstate);
vfm.add(new SeparatorField());
vfm.add(_zippostalcode);
vfm.add(new SeparatorField());
vfm.add(_country);
vfm.add(new SeparatorField());
vfm.add(_phone);
vfm.add(new SeparatorField());
vfm.add(_email);
vfm.add(new SeparatorField());

In the code below we are creating an event handler to check when the button is clicked and throw an event. To do this we create a ChangeListener and attach it to “_submit” Button variable and make it a new FieldChangeListener. The FieldChangeListener is a listener interface for handling field change events. We want to use this to recognize when a form is complete and we need to save the information. When this change is detected it is possible to execute an action of the developers choice. In this case it is a dialog box that is thrown letting us know that the event handler works.

_submit.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Dialog.alert("Button Event Handler Works");
}
});

vfm.add(_submit);
add(vfm);

// Adding Menu Item To Application
addMenuItem(_menuItem);
}

As you can see below, we have instantiated a new MenuItem object within our MyScreen.java file which will hold of of the items in the menu and add new items if we so desire. We start off by declaring this object as private as we do not want any other screens to be able to access it unless they are within the current file. We can then instantiate the object and give the MenuItem object a name to appear on the menu such as “More Info”. We then can run a dialog box to confirm that the menu item is working.

private MenuItem _menuItem = new MenuItem("More Info", 110, 10)
{
public void run()
{
Dialog.inform("We Just Created A Menu Item");
}
};

The method we are going to use below is going to be called when the user presses the blackberry menu button and chooses close. This will override the standard close function and run a dialog box asking if the user truly would like to leave. If the answer is ok then the program is closed. If the user cancels exiting the application then we thank them for taking an interest in the application and let them continue. We have put these dialog boxes here so that we can verify that they are in fact working and are ready to be utilized in the next tutorial.

public void close()
{

if(Dialog.ask(4, "Do You Really Want To Leave Me?") == Dialog.OK)
super.close();
else
Dialog.alert("Thanks For Sticking Around!");
} 
}


With the application now graphically complete you should now know how to create a menu item, override the close function in the Blackberry menu and implement a button click event. With these skills under your belt we are now able to move onto the next part of the tutorial series. The next tutorial will focus on storage of the data we have acquired. Until the next blackberry development 101 tutorial article, Happy Hacking!

Share This