Nov 19, 2009

Help for MIDP friends

eSWT developers usually come from two different backgrounds. Those who are familiar with desktop Java GUIs especially SWT that are starting to do mobile development or seasoned MIDP developers who are looking for advanced UI features for their applications. Looking at the questions coming from the later group of developers on forums, a common theme is about how to implement something that they could do with MIDP UI with eSWT. Here is some help for two cases that are asked often.

How to fix the screen orientation to landscape or portrait? This feature is implemented on MIDP UIs using a Nokia specific Midlet Jad attribute Nokia-MIDlet-App-Orientation. eSWT does not make use of the any of the Jad attributes that are present on the MIDP platform. Instead it provides APIs that enables developers to achieve the same results. This also allows eSWT applications to be easily portable between runtimes. eSWT’s way for achieving fixed screen orientation is using the Screen API. Here is a short snippet that fixes the orientation to portrait for all available screens.
1:     Screen[] screens = MobileDevice.getMobileDevice().getScreens();
2:     for (int i = 0; i < screens.length; i++) {
3:       screens[i].setOrientation(Screen.PORTRAIT);
4:     }
A good thing to remember is some devices may actually plug new screens, for the best results it is a good idea to listen for Screen changes using the listeners from MobileDevice APIs.

How to put an eSWT application to background? On mobile platforms that support background applications MIDP developers achieve sending the application to background by removing the current Displayable. Here is an entry that explains how it is done on MIDP LCDUI. The same can be achieved for eSWT applications by setting the active Shell to minimized. Here is how.
1:         Shell activeShell = display.getActiveShell();
2:         while(activeShell != null ){
3:           activeShell.setMinimized(true);
4:           activeShell = display.getActiveShell();
5:         }
The example snippet takes into account that you may actually have more than one top-level Shell, which the second one may get active once the currently active one is minimized.

I will try to make more of similar kind of posts in the future. Please do comment if you have any suggestions for content. And of course. I continue to keep an eye on the Nokia forums and Eclipse discussion boards.