Simple Java ME Canvas loading bar
Article Metadata
This is an example of a simple canvas based loading bar which you can use as a splash screen for you application. First we create the canvas for the application and get the width and height for the screen
height = getHeight();
width = getWidth();
We then have a factor for the incrementing the loading
factor = width / 4;
We also have a Timer Object
This code comes in the paint method of the canvas
g.setColor(0x00ffccff);
g.fillRect(0, 0, width, height);
g.setColor(0x00ffffff);
g.fillRect(25, height / 2, width - 50, 2);
g.setColor(0x003399ff);
g.drawString("Application name", 100, (height / 2) - 30, Graphics.TOP | Graphics.HCENTER);
g.fillRect(25, height / 2, current, 2);
g.drawString("Loading...", 100, (height / 2) + 20, Graphics.BOTTOM | Graphics.HCENTER);
We then create a class inside the canvas class which will extend the TimerTask class to handle the animation
private class Draw extends TimerTask {
public void run() {
current = current + factor;
if (current > width - 50) {
current = 0;
repaint();
} else {
repaint();
}
}
}
In the constructor of the midlet, we schedule the timer Object to run
repaint();
timer.schedule(new Draw(), 2000, 2000);
We now need to create an object of the canvas in the midlet and display it.


02 Sep
2009
The article provides code for displaying a bar while loading application.
Code presented is out of larger context so understanding of JavaME programming is needed to create a working demo out of it. Author concentrated on the displaying aspect - code that animates bar is not "aware" of loading progress. Nevertheless it is a useful technique to give user something moving when waiting for an app to run. I've seen it several times and it is a good practice IMO, because user has the feedback that app is starting fine, and it helps to hide slowness. Although this is arguably good for user, better make an app faster if possible. Time when app is loading might be also used for displaying useful tips or ads to user, so don't make your app too fast ;).
I'm a little worried about draw class name - I am accustomed that all Java classes start with capital letters.