Help Center

Local Navigation

Indicate activity

You can display a field in your BlackBerry® device application that indicates that work is proceeding. Typically, the field is a spinner, an hour glass, or other similar animated visual cue. The field is implemented by using the ActivityImageField class.
  1. Import the required classes and interfaces.
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.progressindicator.*;
    import net.rim.device.api.ui.container.*;
  2. Create an image that contains frames of an animation and include it in your project. The image must consist of a series of frames of the animation arranged horizontally. The width of the image must be the width of a frame multiplied by the number of frames. For example, the following image has five frames of equal size.

  3. Create a bitmap from the image.
    Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
  4. Create an ActivityIndicatorView object. You can specify a style for the view in the constructor.
    ActivityIndicatorView view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
    To specify a manager to use for layout and focus, provide a second argument when you create the ActivityIndicatorView. If you do not specify a manager, a VerticalFieldManager object is used.
  5. Create a model and a controller.
    ActivityIndicatorModel model = new ActivityIndicatorModel();
    ActivityIndicatorController controller = new ActivityIndicatorController();
  6. Connect the view, model, and controller.
    view.setController(controller);
    view.setModel(model);
    
    controller.setModel(model);
    controller.setView(view);
    
    model.setController(controller);
  7. Create the field that renders the activity from the bitmap.
    view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);
    In this example, the bitmap consists of five frames and is displayed centered in the view's manager.
  8. Add the view to your screen.
    add(view);
  9. Control the activity indication by controlling the view's model, as needed.
    MenuItem _stopIndicator = new MenuItem("Stop spinner", 66000, 0)
    {
        public void run()
        {
            view.getModel().cancel();
        }
    };
        
    MenuItem _resumeIndicator = new MenuItem("Resume spinner", 66010, 0)
    {
        public void run()
        {
            view.getModel().resume();
        }
    }; 
    This example invokes the model's cancel() method to stop the animation. The example invokes the model's resume() method to resume the animation.

Code sample

public class ActivityIndicatorScreen extends MainScreen
{
    ActivityIndicatorView view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
    ActivityIndicatorModel model = new ActivityIndicatorModel();
    ActivityIndicatorController controller = new ActivityIndicatorController();

    public ActivityIndicatorScreen ()
    {
        setTitle("Activity Indicator Demo");
        
        view.setController(controller);
        view.setModel(model);

        controller.setModel(model);
        controller.setView(view);

        model.setController(controller);        

        // Define the indicator image and create a field from it 
        Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
        view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);
        
        // add the view to the screen
        add(view);
 
        MenuItem _stopIndicator = new MenuItem("Stop spinner", 66000, 0)
        {
            public void run()
            {
                view.getModel().cancel();
            }
        };
    
        MenuItem _resumeIndicator = new MenuItem("Resume spinner", 66010, 0)
        {
            public void run()
            {
                view.getModel().resume();
            }
        }; 

        addMenuItem(_stopIndicator);
        addMenuItem(_resumeIndicator);                
    }
}

The Progress Indicator Demo sample application that is included in the BlackBerry® Java® SDK creates and manipulates a variey of activity indicators, including the spinner that is shown above.

Next topic: Indicate progress

Was this information helpful? Send us your comments.