Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Simple Java ME Canvas loading bar

From Wiki
Jump to: navigation, search

This is an example of a simple canvas based loading bar which you can use as a splash screen for you application.

Article Metadata
Created: muiruri (31 Aug 2009)
Last edited: hamishwillee (09 Aug 2013)

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

Timer timer = new Timer();

This code comes in the paint method of the canvas

g.fillRect(0, 0, width, height);
g.fillRect(25, height / 2, width - 50, 2);
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;
} else {

In the constructor of the midlet, we schedule the timer Object to run

timer.schedule(new Draw(), 2000, 2000);

We now need to create an object of the canvas in the midlet and display it.

This page was last modified on 9 August 2013, at 05:15.
42 page views in the last 30 days.