Auto Rotation on Blackberry Programming - blackberry

I make code to Blackberry Storm. When my application in horizontal display (480x360), it's work. But when the Blackberry tilt into Vertical (360x480), the picture is cut off. So I was asking how to set up so that at the time of rotation, also resize the picture? is there any method to check if BlackBerry again horizontal or vertical display?
Thanks.

Here are two things, either you will lock screen orientation or you will support in your application.
Code sample: Retrieving screen orientation
switch(Display.getOrientation())
{
case Display.ORIENTATION_LANDSCAPE:
Dialog.alert("Screen orientation is landscape"); break;
case Display.ORIENTATION_PORTRAIT:
Dialog.alert("Screen orientation is portrait"); break;
case Display.ORIENTATION_SQUARE:
Dialog.alert("Screen orientation is square"); break;
default:
Dialog.alert("Screen orientation is not known"); break;
}
Code sample: Forcing portrait view in a BlackBerry API application
// Use code like this before invoking UiApplication.pushScreen()
int direction = Display.DIRECTION_NORTH;
Ui.getUiEngineInstance().setAcceptableDirections(direction);
You you want to handle the images and other graphics setup on orientation change then you can do the following changes in your code.
Override the sublayout method, in your MainScreen subclass.
protected void sublayout(int arg0, int arg1) {
// do all the
super.sublayout(arg0, arg1);
}
Check for the orientation changes, rearrange the UI. Usages of relative layout is recommended for such things.
Hope, this might help you out. For more info visit Specifying_display_direction_of_screen
Edit: override sublayout() and then write the code specific to orientation
public void sublayout(int width, int height) {
switch(Display.getOrientation())
{
case Display.ORIENTATION_LANDSCAPE:
// write the piece of code for refreshing the screen UI which screen orientation is landscape
break;
case Display.ORIENTATION_PORTRAIT:
// write the piece of code for refreshing the screen UI which screen orientation is portrait
break;
}
super.sublayout(width, height);
}
Edit 2:
you were going wrong because of UI event lock, now you do the following changes to your code.
public void sublayout(int maxWidth, int maxHeight) {
int displayWidth = deviceWidth;
int displayHeight = deviceHeight;
switch (Display.getOrientation()) {
case Display.ORIENTATION_LANDSCAPE:
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Screen orientation is landscape");
// here you need to change your uI code as you want to do in for landscape mode
// you may need to delete and add the UI comps manually
// if the components added to absolute layout then just refresh the screen it will auto adjust with your new screen size
}
});
break;
case Display.ORIENTATION_PORTRAIT:
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Screen orientation is PORTRAIT:");
// here you need to change your uI code as you want to do in for PORTRAIT mode
// you may need to delete and add the UI comps manually
// if the components added to absolute layout then just refresh the screen it will auto adjust with your new screen size
}
});
break;
}
super.sublayout(displayWidth, displayHeight);
setExtent(displayWidth, displayHeight);
}

Well I faced this problem before and solved it. The solution is not to "oblige the blackberry screen not to rotate". I made this before and it is annoying. Well the solution to this is override the subLayout method of the main screen and all the managers included in the main screen. This method is always called when a rotation occurs.
Your code will looks like follows:
public void sublayout(int width, int height) {
changeImage(); // this method will change your image x and y and then draw it again
super.sublayout(width, height);
}

Related

control screen orientation blackberry

I want to display an alert in every time when the user change the orientation of the mobile. There is a method to capture this mouvement?
I use Display.getOrientation();. But it always returns 32 in different orientation.
You get a call to sublayout() when orientation changes. You can save the displaywidth to a variable, and if the value has changed in sublayout() , you know the orientation has changed.
like this.
protected void sublayout(int width, int height) {
if(Display.getWidth()==oldWidth)
{
//no change
}
else
{
// orientation changed
}
oldWidth = Display.getWidth();
super.sublayout(width, height);
}
Note: when you check orientation you have to close SLIDER in 9800 simulator other wise it wont work
try following code :
public class checkOrientation
{
VerticalFieldManager manager;int ori;
public checkOrientation()
{
ori=Display.getOrientation();
manager=new VerticalFieldManager()
{
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
if(ori!=Display.getOrientation()){
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
ori=Display.getOrientation();
Dialog.inform(""+Display.getOrientation());
}
});
}
}
};
add(manager);
}
}

BlackBerry: setting the height of a field manager without manually building the layout

I want to set the height of a manager so that my tab bar sits perfectly beneath the manager at the bottom of the screen.
I'd add my vertical field manager which holds all the content, then I add(tabbar).
The problem is that when I use the following, all the fields disappear. However, the height is set the way I want it.
bottom_vfm = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
setExtent(maxWidth, 200);
}
};
Do I have to manually setChildPosition and layoutChild for every field? Is there any way around it?
have u tried like this?
bottom_vfm = new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight){
super.sublayout(maxWidth,200);
setExtent(maxWidth,200);
}
};

How get height of title screen on blackberry screen?

Why getHeight after setTitle equal null?
How do I get the actual height value?
public class ActivityScreen extends MainScreen {
/**
* Creates new instance Activity screen
*/
public ActivityScreen() {
super();
TitleFieldManager titleField = new TitleFieldManager(Display.getWidth());
super.setTitle(titleField);
if (titleField.getHeight() == 0) {
// Why titleField.getHeight() == 0 ?
}
}
}
Because you are calling titleField.getHeight() in the screen's constructor. At this point the UI framework has not passed the layouting/measuring process for the screen yet. If for instance, you will call the same titleField.getHeight() after at least one screen's paint(Graphics graphics) call has been executed you'll be able to get a non-zero value. All layouting/measuring is guaranteedly passed BEFORE the screen content can be drawn.
Check the Manager.sublayout(int width, int height) API. MainScreen is also a Manager. So at some point AFTER the screen has been constructed, but BEFORE it is painted, the UI framework calls its sublayout(int width, int height) where all layouting/measuring happens (all child fields get their sizes).
I'm not 100% sure of this, but I think the height of the element is calculated when the manager that contains it is laid out.
Try pushing the screen that contains that title and then calling titleField.getHeight()

Blackberry storm - update layout on tilt

have developed an app for BB storm while tilting the device the background image of the app screen does not matches with the screen size, i have tried with the sublayout method
public void sublayout(int width, int height)
{
//update scrren layout based on orientation
if(Display.getOrientation()== Display.ORIENTATION_LANDSCAPE)
{
invalidate();
}
else if(Display.getOrientation()== Display.ORIENTATION_PORTRAIT)
{
invalidate();
}
super.sublayout(width, height);
}
Still not successfull can any one help to sort out this tilt issue in BB storm
Thanks
SujithRavindran
Rapidvaluesolutions
You are calling invalidate() but not doing anything to change the actual background image. You'll probably want to change the image for your BitmapField (or whatever you're using for the background), and then call invalidate().
You don't need to call invalidate() as part of your sublayout method. Layout happens first, and then your screen is automatically invalidated by the system.
Also, updating the layout in your sublayout method will trigger a second layout.

Blackberry setting the position of a RichtextField in FullScreen

I am writing an application in BlackBerry, where I want to do some custom painting at the top portion of the screen in the paint method of FullScreen and at the same time, I want a RichtextField positioned at the lower portion of the screen. I tried using setPosition methods in the Field class, but to no avail. So how do I set the position of the RichtextField that is added to the FullScreen class?
You can use a SpacerField for that purpose:
class SpacerField extends Field {
int localWidth, localHeight;
SpacerField(int width, int height) {
super(Field.NON_FOCUSABLE);
localWidth = width;
localHeight = height;
}
protected void layout(int width, int height) {
// TODO Auto-generated method stub
setExtent(localWidth, localHeight);
}
protected void paint(Graphics graphics) {
}
public int getPreferredWidth() {
return localWidth;
}
public int getPreferredHeight() {
return localHeight;
}
}
and add it to your Screen before your RichTextField. Be sure to give a suitable width (Display.getWidth() ?) and height when constructing the SpacerField.
Note: I had found the code at this forum discussion a few months ago when I needed to do something similar.
The best way to position objects is to extend a Manager and use it to position and size the objects the way you want. Check the documentation for net.rim.device.api.ui.Manager and net.rim.device.api.ui.Field for information on how manager control their children.

Resources