dodevelopnshare


Leave a comment

ProgressDialog in Blackberry

Being a blackberry developer since last 4 years,  I know people face problems to show a Progress Dialog or Wait Screen or Loading Dialog, while execution of some long processing in a background thread.

There are several good examples over the BB Support Forums and StackOverflow pertaining to the above issue. Today, I would like to share the code I use for this. I have 2 classes for this for this functionality.

    1. ProgressDialog extends PopupScreen
    2. ProgressSpinner extends Field
    3. loader.png (attached)

loader

ProgressDialog.java

import net.rim.device.api.system.Display;

import net.rim.device.api.ui.Color;

import net.rim.device.api.ui.DrawStyle;

import net.rim.device.api.ui.Graphics;

import net.rim.device.api.ui.UiApplication;

import net.rim.device.api.ui.component.LabelField;

import net.rim.device.api.ui.container.HorizontalFieldManager;

import net.rim.device.api.ui.container.PopupScreen;

import net.rim.device.api.ui.container.VerticalFieldManager;

public class ProgressDialog extends PopupScreen {

private ProgressSpinner mProgressSpinner;

public ProgressDialog () {

this(Strings.PLEASE_WAIT_TEXT);

}

public ProgressDialog (String text) {

super(new VerticalFieldManager(), DEFAULT_CLOSE);

mProgressSpinner = new ProgressSpinner(true);

HorizontalFieldManager hfm = new HorizontalFieldManager();

hfm.add(mProgressSpinner);

LabelField lblText = new LabelField(text, DrawStyle.HCENTER | FIELD_VCENTER) {

protected void paint(Graphics graphics) {

super.paint(graphics);

graphics.setColor(Color.WHITE);

}

};

lblText.setPadding(0, 0, 0, 5);

hfm.add(lblText);

hfm.setPadding(5, 5, 5, 5);

add(hfm);

}

public void show(boolean show) {

if (show) {

if (!isDisplayed()) {

UiApplication.getUiApplication().pushScreen(this);

mProgressSpinner.startProgressbar();

}

else {

mProgressSpinner.stopProgressbar();

if (isDisplayed()) {

close();

}

}

}

public void paint(Graphics g) {

g.pushContext(0, 0, Display.getWidth(), Display.getHeight(), 0, 0);

super.paint(g);

g.popContext();

}

}

ProgressSpinner.java

import java.util.Timer;

import java.util.TimerTask;

import net.rim.device.api.system.Bitmap;

import net.rim.device.api.ui.Field;

import net.rim.device.api.ui.Graphics;

public class ProgressSpinner extends Field {

private int mFrameCounts;

private int mFrameWidth;

private int mFrameHeight;

private int mCurrentFrame;

private Timer mTimer;

private boolean mIsLarge;

private Bitmap mProgressImage;

private static Bitmap BIG;

private static Bitmap SMALL;

public ProgressSpinner(boolean large) {

mIsLarge = large;

if (BIG == null) {

BIG = Bitmap.getBitmapResource(“loader.png”);

SMALL = Bitmap.getBitmapResource(“loader.png”);

}

mProgressImage = large ? BIG : SMALL;

mFrameCounts = 12;

mFrameHeight = mProgressImage.getHeight();

mFrameWidth = mProgressImage.getWidth() / mFrameCounts;

}

protected void layout(int width, int height) {

setExtent(mFrameWidth, mFrameHeight);

}

public void paint(Graphics g) {

g.drawBitmap(0, 0, mFrameWidth, mFrameHeight, mProgressImage, mCurrentFrame * mFrameWidth, 0);

}

public int getPreferredHeight() {

return mFrameHeight;

}

public int getPreferredWidth() {

return mFrameWidth;

}

public void startProgressbar() {

final ProgressSpinner mProgBar = this;

TimerTask mTimerTask = new TimerTask() {

public void run() {

mProgBar.invalidate();

if (mCurrentFrame == mFrameCounts – 1) {

mCurrentFrame = 0;

}

mCurrentFrame++;

}

};

mTimer = new Timer();

mTimer.schedule(mTimerTask, 0, mIsLarge ? 100 : 200);

}

public void stopProgressbar() {

if (mTimer != null) {

mTimer.cancel();

mTimer = null;

}

}

protected void onUndisplay() {

stopProgressbar();

super.onUndisplay();

}

}

How to use?

ProgressDialog progress = new ProgressDialog ();

progress.show(true); //to show

progress.show(false);

This works awesome. Hope, You will be loving it too…!! Let me know how did it worked for you?