Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create BlackBerry application with java

The article will assist the developers that are at the beginner level to create the simplest of the BlackBerry Applications with the help of Java language.

Pre-requisite before you begin:

Before you begin, you would hear lot of requirements to download several tools but the article demands no requirement to download eclipse or BlackBerry JDE. The only thing that is required to be downloaded BlackBerry Plug-In for eclipse. It comprises of Eclipse IDE, BlackBerry plug-in and a simulator to test and debug your applications.

Creating a Project

Post installing the plug-in, just open the same and specify your workspace folder. Here you need to select "WorkBench" at the home screen that is being displayed. Now do a right click on the project explorer and then select New->BlackBerry Project, name your project here and then access the Finish Button.

Creating Project

Figura 1: Creating Project

The thing to notice here is that the project has the following files pre-built.

Folders

Figure 2: Folder’s

In the BlackBerry_App_Descriptor.xml, we can specify the name of the application, its version, description and many more attributes. One can also give the application launch icon that is displayed on the menu of the blackberry application. In order to add the icon, the first thing is to add it to res folder and then choose it from the "Add" button present in the panel of the Application Icons. On verifying the rollover, the icon that was specified, obviously a different icon, will be displayed when the application is hovered in the BlackBerry app menu.

MyApp.java

The basic and fundamental task of MyApp.java is to come up with a new instance of User Interface Application and then push a screen to stack for rendering.

MyScreen.java

This would be your main screen that is pushed onto the stack. Now we will specify our layout and implement methods as per the needs.

Creating a simple login screen

In case you are deciding to execute the project now on the simulator, a dos like icon would be displayed to you in the application menu that is named as "TutorialApp". On opening the same, a white screen will be displayed with the title appearing as "MyTitle". Let us see the concept of log-in-screen that is actually required in almost all the applications available.

The first step would be to alter the title by altering the setTitle() method. Post doing this, we will add a LabelField much similar to JLabel in java so as to display a very easy heading.

The next step is to incorporate add two HorizontalFieldManagers that will be added with two Labels("Username" and "Password") and one BasicEditField that would be much similar to JTextField in java and one PasswordEditField that will be required to get userinput.

The below represents the entire code:

Listing 1: Full Source Code

package mypackage;
 
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.PasswordEditField;
 
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
BasicEditField username;
PasswordEditField password;
 
/**
* Creates a new MyScreen object
*/
public MyScreen()
{        
// Set the displayed title of the screen       
setTitle("Welcome");
 
LabelField login = new LabelField("Log-In", LabelField.FIELD_HCENTER);
login.setFont(Font.getDefault().derive(Font.BOLD, 30));
login.setMargin(10, 0, 20, 0); //To leave some space from top and bottom
 
 
HorizontalFieldManager user = new HorizontalFieldManager();
user.setMargin(0, 0, 10, 0);
HorizontalFieldManager pass = new HorizontalFieldManager();
pass.setMargin(0, 0, 20, 0);
HorizontalFieldManager btns = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
 
LabelField usernameTxt = new LabelField("Username :");
LabelField passwordTxt = new LabelField("Password :");
 
  username = new BasicEditField();
  password = new PasswordEditField();
 
ButtonField loginBtn = new ButtonField("Log-In", ButtonField.CONSUME_CLICK);
ButtonField exitBtn = new ButtonField("Exit", ButtonField.CONSUME_CLICK);
 
user.add(usernameTxt);
user.add(username);
pass.add(passwordTxt);
pass.add(password);
btns.add(loginBtn);
btns.add(exitBtn);
add(login);
add(user);
add(pass);
add(btns);
}
}
}
Welcome Page

Figure 3: Welcome Page

We are ready with our layout however we will introduce one more java class in our src folder. This will get open on receiving the correct username and password. Now do a right click on mypackage->New->Class.

The next we would incorporate some functionality so as to receive user input and then open the new screen. Below represents the code where we defined exitBtn

loginBtn.setChangeListener(btnlistener);
exitBtn.setChangeListener(btnlistener2);

The following codes needs to be written post closing curly brace of the constructor and before the class ends.

Come up with a java source code.

FieldChangeListener btnlistener = new FieldChangeListener() {
 
public void fieldChanged(Field field, int context) {
//Open a new screen
String uname = username.getText();
String pwd = password.getText();
 
//If there is no input
if (uname.length() == 0 || pwd.length()==0)
Dialog.alert("One of the textfield is empty!");
else if (uname.equals("user") && pwd.equals("admin"))
UiApplication.getUiApplication().pushScreen(new NewScreen()); //Open a new Screen
else
Dialog.alert("Username or password not found!");
}
 
};
 
FieldChangeListener btnlistener2 = new FieldChangeListener() {
 
public void fieldChanged(Field field, int context) {

//Open a new screen if variables are not empty
 
//To quit application
System.exit(0);        
}
};
Validation Alert

Figure 4: Validation Alert

Wrong login alert

Figure 5: Wrong login alert

When the user does not input anything, the same is being notified via the help of dialog. Also, you will get the notification or alert in case the username and password is not matched. And in case the username and password is matched, a new screen is displayed.

Conclusion

The article here explained to develop the simplest of the BlackBerry Applications with the help of Java.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login