Monday, May 29, 2017

Developing a simple BlackBerry Application through Java

 How to develop a simple BlackBerry application in java, for absolute beginners.

Setting Up

There is no need to download eclipse or BlackBerry JDE, just download BlackBerry Plug-In for eclipse from here. It includes Eclipse IDE, BlackBerry plug-in and a Simulator to test and debug your apps.

Creating a Project

After you have installed the plug-in, open it and specify your workspace folder. Select "WorkBench" at start screen that appears. Right click on the project explorer and select New>BlackBerry Project. Name your project and click finish.




You will notice your project has the following files pre-built.





In the BlackBerry_App_Descriptor.xml you can define your app's name, version, description etc. You can also specify the app's launch icon that will appear on the blackberry app menu. For adding icon, you would first have to add it to res folder then select it from the "Add" button in Application Icons panel. If you check rollover, the specified icon(obviosly a differrent icon) will appear when the app is hovered in the BlackBerry app menu.

MyApp.java

It's simple basic work is to create a new instance of UiApplication and push a screen to stack for rendering. Leave it as it is.

MyScreen.java

This is the main screen which is push onto the stack. In this class we will define our layout and implement methods as required.

Creating a simple login screen
If you would run your project now on the simulator, you would see a dos like icon in the app menu named "TutorialApp". If you will open it, a white screen will appear with the title "MyTitle". Many tutorials will show how to develop a HelloWorld app or a simple calculator, but I will show something that is required in almost every app i.e a log-in screen.

First we will change our title by editing the setTitle() method. Then we will add a LabelField (similar to JLabel in java) for displaying a simple heading. After that we will add two HorizontalFieldManagers in which we will put our two Labels("Username" and "Password") and one BasicEditField (similar to JTextField in java) and one PasswordEditField in which we will get our userinput.

Here's the whole 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);
}
}
 

Our layout is ready, but we will add another java class in our src folder which will open if we receive the correct username and password. Right Click on mypackage>New>Class.

Now we will add some functionality to receive user input and open our new screen. Below the line in which we declared the exitBtn write these two lines of code

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

Now, after the closing curly brace of the constructor and before the class ends write following codes to finish our application.

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);        
}
};
 

When the textfields are empty i.e user does not input any thing, then user is notified of the same through a dialog. There is also an alert if the username and password is not matched. A new screen is visible if the username and password is correct.

I hope this post will help you understand the basics of BlackBerry Java Development. If you have any query please comment.
 

No comments:

Post a Comment