Home » Odeon Blogs » Bogdan, Methodical Coder »

BlackBerry Screen Transition Sample Code

BlackBerry Screen Transition Sample Code

Develop on Blackberry!

Screen Transition Sample

The code that I'm about the share and go through illustrates a slide and a fade transition. When the user opens the application, the first screen appears on the BlackBerry® device and displays a button. When the user clicks the button, a second screen slides onto the display from the right. The second screen automatically fades off of the display after two seconds.

First, let's add the imports:

  1. import net.rim.device.api.ui.*;
  2. import net.rim.device.api.ui.component.*;
  3. import net.rim.device.api.ui.container.*;
  4. import net.rim.device.api.ui.decor.*;

Now, let's continue with the application:

  1. public class ScreenTransitionSample extends UiApplication implements FieldChangeListener {
  2. private Screen secondaryScreen;
  3. private Runnable popRunnable;

As you can see, we initialized two local variables. As the official documentation says, you must explicitly initialize local variables in a method. We've initialized the secondaryScreen variable, because Screen is the base class for all screens and popRunnable. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. We'll implement run later in the code.

Any BlackBerry application that provides a user interface must extend the UiApplication class. Our class also implements FieldChangeListener. This is because Classes that want to handle change events should implement this interface.

  1. public static void main(String[] args) {
  2. ScreenTransitionSample theApp = new ScreenTransitionSample ();
  3. theApp.enterEventDispatcher();
  4. }

Class ScreenTransitionSample must have one method: main(String[] args) which is the entry point into our application. We need to create an instance of our application and start it by getting it on the event thread. Next, we will create the secondary screen:

  1. public ScreenTransitionSample () {
  2. secondaryScreen = new FullScreen();
  3. secondaryScreen.setBackground(
  4. BackgroundFactory.createSolidBackground(Color.LIGHTBLUE) );
  5. LabelField labelField = new LabelField("The screen closes automatically in two seconds by using a fade transition");
  6. secondaryScreen.add(labelField);

and create the transition object:

  1. TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
  2. transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
  3. transition.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
  4. transition.setIntAttribute(TransitionContext.ATTR_STYLE, TransitionContext.STYLE_PUSH);

which helps us create the animation. Next, we create an engine object:

  1. UiEngineInstance engine = Ui.getUiEngineInstance();

As the official documentation says, a UI engine maintains a stack of Screen objects. As it pushes screens onto the stack, it draws them on top of any other screens already on the stack. When the application pops a screen off the stack, it redraws the underlying screens as necessary. Only the screen on the top of the stack receives input events.

Now, apply the transition, create a new one and apply that too onto the secondary screen:

  1. engine.setTransition(null, _secondaryScreen, UiEngineInstance.TRIGGER_PUSH, transition);
  2. transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
  3. transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
  4. transition.setIntAttribute(TransitionContext.ATTR_KIND, TransitionContext.KIND_OUT);
  5. engine.setTransition(secondaryScreen, null, UiEngineInstance.TRIGGER_POP, transition);

Now, create the base screen and attach a button:

  1. MainScreen baseScreen = new MainScreen();
  2. baseScreen.setTitle("Screen Transition Sample");
  3. ButtonField buttonField = new ButtonField("View Transition", ButtonField.CONSUME_CLICK) ;
  4. buttonField.setChangeListener(this);
  5. baseScreen.add(buttonField);

Push the screen onto the stack and paint it:

  1. pushScreen(baseScreen);

then continue with the run method that we discussed about at the beggining and the rest of the code:

  1. popRunnable = new Runnable() {
  2. public void run() {
  3. popScreen(secondaryScreen);

here we remove the base screen from the display stack, and updates the secondary screen.

  1. }
  2. };
  3. }
  4. public void fieldChanged(Field field, int context) {
  5. pushScreen(secondaryScreen);
  6. invokeLater(popRunnable, 2000, false);
  7. }
  8. }

We added a listener onto the button and now we took care of the action that needs to be taken on it.

That's about it. Here's the full code sample:

  1. import net.rim.device.api.ui.*;
  2. import net.rim.device.api.ui.component.*;
  3. import net.rim.device.api.ui.container.*;
  4. import net.rim.device.api.ui.decor.*;
  5. public class ScreenTransitionSample extends UiApplication implements FieldChangeListener {
  6. private Screen _secondaryScreen;
  7. private Runnable _popRunnable;
  8. public static void main(String[] args) {
  9. ScreenTransitionSample theApp = new ScreenTransitionSample ();
  10. theApp.enterEventDispatcher();
  11. }
  12. public ScreenTransitionSample () {
  13. secondaryScreen = new FullScreen();
  14. secondaryScreen.setBackground( BackgroundFactory.createSolidBackground(Color.LIGHTBLUE) );
  15. LabelField labelField = new LabelField("The screen closes automatically in two seconds by using a fade transition");
  16. secondaryScreen.add(labelField);
  17. TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
  18. transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
  19. transition.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
  20. transition.setIntAttribute(TransitionContext.ATTR_STYLE, TransitionContext.STYLE_PUSH);
  21. UiEngineInstance engine = Ui.getUiEngineInstance();
  22. engine.setTransition(null, _secondaryScreen, UiEngineInstance.TRIGGER_PUSH, transition);
  23. transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
  24. transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
  25. transition.setIntAttribute(TransitionContext.ATTR_KIND, TransitionContext.KIND_OUT);
  26. engine.setTransition(_secondaryScreen, null, UiEngineInstance.TRIGGER_POP, transition);
  27. MainScreen baseScreen = new MainScreen();
  28. baseScreen.setTitle("Screen Transition Sample");
  29. ButtonField buttonField = new ButtonField("View Transition", ButtonField.CONSUME_CLICK) ;
  30. buttonField.setChangeListener(this);
  31. baseScreen.add(buttonField);
  32. pushScreen(baseScreen);
  33. }
  34. }
  35. popRunnable = new Runnable() {
  36. public void run() {
  37. popScreen(_secondaryScreen);
  38. }
  39. };
  40. public void fieldChanged(Field field, int context) {
  41. pushScreen(_secondaryScreen);
  42. invokeLater(_popRunnable, 2000, false);
  43. }

For more examples you can check out the official documentation on http://docs.blackberry.com/en/developers/subcategories/?userType=21&category=Development+Guides


Category: BlackBerry


Tagged as: BlackBerry screen transition

Discussion

  1. ANGRY DUDE on Apr 14, 2012 - 22:55 said:

    This doesnt work.. it gives too many errors


  2. Amirthan on Sep 13, 2012 - 2:00 said:

    it works..


  3. Lwazi on Aug 08, 2013 - 2:50 said:

    Awesome code, thanks a lot it works great. If you don't mind I edited it abit because the full sample didn't work at first so I had to make some small changes
    here's the edited code.

    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.ui.decor.*;

    public class ScreenTransitionSample extends UiApplication implements FieldChangeListener {

    private Screen secondaryScreen;
    private Runnable popRunnable;

    public static void main(String[] args) {
    ScreenTransitionSample theApp = new ScreenTransitionSample ();
    theApp.enterEventDispatcher();
    }
    public void fieldChanged(Field field, int context) {
    pushScreen(secondaryScreen);
    invokeLater(popRunnable, 2000, false);
    }

    public ScreenTransitionSample () {
    secondaryScreen = new FullScreen();
    secondaryScreen.setBackground( BackgroundFactory.createSolidBackground(Color.LIGHTBLUE) );
    LabelField labelField = new LabelField("The screen closes automatically in two seconds by using a fade transition");
    secondaryScreen.add(labelField);
    TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
    transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
    transition.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
    transition.setIntAttribute(TransitionContext.ATTR_STYLE, TransitionContext.STYLE_PUSH);
    UiEngineInstance engine = Ui.getUiEngineInstance();
    engine.setTransition(null, secondaryScreen, UiEngineInstance.TRIGGER_PUSH, transition);
    transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
    transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
    transition.setIntAttribute(TransitionContext.ATTR_KIND, TransitionContext.KIND_OUT);
    engine.setTransition(secondaryScreen, null, UiEngineInstance.TRIGGER_POP, transition);
    MainScreen baseScreen = new MainScreen();
    baseScreen.setTitle("Screen Transition Sample");
    ButtonField buttonField = new ButtonField("View Transition", ButtonField.CONSUME_CLICK) ;
    buttonField.setChangeListener(this);
    baseScreen.add(buttonField);
    pushScreen(baseScreen);
    popRunnable = new Runnable() {
    public void run() {
    popScreen(secondaryScreen);
    }
    };
    }

    }






Leave a Comment :

(required)


(required)




(required)








Page generated in: 0.13s