Showing posts with label snippets. Show all posts
Showing posts with label snippets. Show all posts

Thursday, April 21, 2011

Don't allow application to go back

Just add following code to your Screen class:

public boolean onClose() {

return false;

}

Sunday, March 6, 2011

Resize header dynamically based on Display width

We need to prepare 3 images:




public class HeaderManager extends HorizontalFieldManager {

public HeaderManager() {

super(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.FIELD_RIGHT);



this.setBackground(BackgroundFactory.createSolidBackground(Color.RED));



Bitmap image;

Bitmap leftImage = Bitmap.getBitmapResource("header_left.png");

this.add(new BitmapField(leftImage));


image = Bitmap.getBitmapResource("header_middle.png");

Bitmap rightImage = Bitmap.getBitmapResource("header_right.png");


this.add(new BitmapField(resizeBitmap(image, Display.getWidth() - leftImage.getWidth() - rightImage.getWidth(), image.getHeight())));


VerticalFieldManager vert = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.FIELD_RIGHT);


vert.add(new BitmapField(rightImage, Field.FIELD_RIGHT));


this.add(vert);


}



public static Bitmap resizeBitmap(Bitmap image, int width, int height) {

int imageWidth = image.getWidth();

int imageHeight = image.getHeight();


// Need an array (for RGB, with the size of original image)

int rgb[] = new int[imageWidth * imageHeight];


// Get the RGB array of image into "rgb"

image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);


// Call to our function and obtain rgb2

int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);


// Create an image with that RGB array

Bitmap temp2 = new Bitmap(width, height);


temp2.setARGB(rgb2, 0, width, 0, 0, width, height);


return temp2;

}


private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {

int out[] = new int[x2 * y2];

for (int yy = 0; yy <>

int dy = yy * y / y2;

for (int xx = 0; xx <>

int dx = xx * x / x2;

out[(x2 * yy) + xx] = ini[(x * dy) + dx];

}

}

return out;

}

}

Resize bitmap

public static Bitmap resizeBitmap(Bitmap image, int width, int height) {

int imageWidth = image.getWidth();

int imageHeight = image.getHeight();


// Need an array (for RGB, with the size of original image)

int rgb[] = new int[imageWidth * imageHeight];


// Get the RGB array of image into "rgb"

image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);


// Call to our function and obtain rgb2

int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);


// Create an image with that RGB array

Bitmap temp2 = new Bitmap(width, height);


temp2.setARGB(rgb2, 0, width, 0, 0, width, height);


return temp2;

}


private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {

int out[] = new int[x2 * y2];

for (int yy = 0; yy <>

int dy = yy * y / y2;

for (int xx = 0; xx <>

int dx = xx * x / x2;

out[(x2 * yy) + xx] = ini[(x * dy) + dx];

}

}

return out;

}

Thursday, March 3, 2011

Remove "Changes Made!" message when hitting back button

To get rid following dialog:


Put following code inside Screen class:

public boolean onClose(){

setDirty(false);

return super.onClose();

}



Tuesday, March 1, 2011

Go to previous screen(s)

//We have screens A -> B -> C

//Current screen is C

//Go to previous screen (B)

UiApplication.getUiApplication().popScreen(this);


//Go to screen A

UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());

Show alert in blackberry

protected static void showError(final String message) {

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

Dialog.alert(message);

}

});

}