Codename One : Adding the splash screen
A mobile app takes some time to start up, especially the first time it runs on a device. This is often referred to as a cold start. So, a common user experience for those apps is to display a loading (splash) screen while it is initializing. Splash screens may display start up progress to the user, or it may display general branding informations (company’s logo, developers team name…).
This tutorial helps you implement a splash screen for your codename one app. All it takes is these two steps :
-
- Adding a SplachScreen.java class that’s a Form to be displayed first.
- Switching to the mainForm.java after a seconds delay.
Notice that we’ll be working on the same project we started on the previous turorials. But, you can always follow up without turning back to them if you need to.
First add a SplashScreen.java class. We’ll add a background image to be shown as our “splash screen’s content” , but you still can add the content you need to, maybe an Image and a label with some infomation, or maybe animate the label somehow, it will be all coded in this class. In our case we’ll only try to add the background :
public class SplashScreen extends Form { public SplashScreen() throws IOException{ this.getUnselectedStyle().setBgImage(Image.createImage("/splashScreen.png")); } }
Then, in our main class (Shelfie.java) , we’ll add the following:
// splash loginForm = new Login(); SplashScreen sps = new SplashScreen(); sps.show(); // after splash new java.util.Timer().schedule( new java.util.TimerTask() { @Override public void run() { loginForm.show(); } }, 2000 );
That’s ALL it takes to add a splash screen to a codename one app. There are obviously different ways to do that, and you can add the content you desire to the SplashScreen.java form. See you in another easy tutorial coders !
ou on met la ressource image ?
if you copy paste or drag the image in your project it will be automatically placed under the and you can set it with .setBgImage(Image.createImage(“/myImg.png”))
Or you can create a new package, name it resources for example and place all your images there, u’ll change the path then to : .setBgImage(Image.createImage(“/resources/myImg.png”))
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(…) returned error code 5.
ERROR: the start method can’t declare a throws clause