All the native BlackBerry applications have a license agreement that pops up looking like this. With the class I am including below it should be very easy to create your own. Resource files should be used. It is simple to create them by right clicking on your “res” folder and selecting “BlackBerry Resource File” from the menu. This will also create the class in “.locale_interfaces” used to access those resources. After adding the required keys you should change the license agreement value to “Convert to Multiple Values” and then add the agreement within the popup. The file that the below usage example uses is:

LicensePopup.java

I will include the usage first as it is a little more complex. The popup must be called prior to entering the main thread, or event dispatcher. The way this is done is as follows:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
protected static ResourceBundle _resources = ResourceBundle.getBundle(BBiCalResource.BUNDLE_ID, BBiCalResource.BUNDLE_NAME);
/**
* Is synonymous with the main function. I separated it and the main function calls run().
*/
public void run(){
	invokeLater(new Runnable() {
		public void run() {
			LicensePopup popupDialog = new LicensePopup(_resources.getStringArray(BBiCalResource.ABOUT_LICENSE),
					_resources.getString(BBiCalResource.MENUITEM_ACCEPT), _resources.getString(BBiCalResource.MENUITEM_DECLINE), 6);
			pushGlobalScreen(popupDialog, 1, UiEngine.GLOBAL_MODAL);
 
			if(!popupDialog.isLicenseAccepted()) {
				System.exit(0);
			}
			else {
				//TODO persistant store that the license was accepted.
				requestForeground();
				pushScreen( new HomeScreen() );
			}
		}
        });
 
	enterEventDispatcher();
}

Below is the license agreement class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ca.dftr.lib.ui.components;
 
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
 
/**
 * Pop up screen that looks like the default BlackBerry License screen
 * @author deforbes
 */
public class LicensePopup extends PopupScreen {
    private ButtonField _btnOkay;
    private ButtonField _btnCancel;
    private RichTextField _licenseTextField;
    private boolean _accepted;
 
    /**
     * Create the License Popup
     * @param licenseText The actual License as a string array (each line is a part of the array). Best to use a resource array
     * @param acceptBtnText Text on the accept button
     * @param declineBtnText Text on the decline button
     * @param fontSize Size of the font in the popup. (RIM uses 6)
     */
    public LicensePopup(String[] licenseText, String acceptBtnText, String declineBtnText, int fontSize) {
        super(new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR));
 
        if (fontSize <= 0){
        	fontSize = 6;
        }
        Font font = Font.getDefault().derive(Font.PLAIN, fontSize, Ui.UNITS_pt);
 
        _licenseTextField = new RichTextField(RichTextField.READONLY);
        for(int i=0; i<licenseText.length; i++) {
        	_licenseTextField.insert(licenseText[i]);
            if(i < licenseText.length - 1) {
            	_licenseTextField.insert("\n");
            }
        }
        _licenseTextField.setFont(font);
        _licenseTextField.setEditable(false);
 
        _btnOkay = new ButtonField(acceptBtnText, ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER);
        _btnOkay.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
            	setAccepted(true);
            }});
        _btnCancel = new ButtonField(declineBtnText, ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER);
        _btnCancel.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
            	setAccepted(false);
            }});
 
        add(_licenseTextField);
        add(new SeparatorField());
        add(_btnOkay);
        add(_btnCancel);
    }
 
    /**
     * Set whether the user clicked accept or decline
     * @param accepted Whether user accepted the license
     */
    private void setAccepted(boolean accepted) {
        _accepted = accepted;
        this.close();
    }
 
    /**
     * @return Whether or not the user accepted the license agreement
     */
    public boolean isLicenseAccepted() {
        return _accepted;
    }
}

Related Posts: