Custom Edit Field Creation problem - blackberry

I have created one screen in which there is background.and on that i am creating a Custom Edit Fields with one button.now my problem is that my edit field is displaying on upper side.i want to make to the center of the screen.what modifications i have to do so.i am giving my code snippet.
public class CancelBooking extends MainScreen implements FieldChangeListener {
public LabelField heading;
ButtonField next;
String emp_id,emp_name,salary;
public CancelBooking(String id,String name,String sal) {
// TODO Auto-generated constructor stub
emp_id=id;
emp_name=name;
salary=sal;
}
// protected void sublayout(int width, int height){
// super.sublayout(Display.getWidth(), height);
// setExtent(Display.getWidth(), this.getPreferredHeight());
// int leftPadding = Display.getWidth()/2 -this.getPreferredWidth()/2;
// int rightPadding = Display.getWidth() - leftPadding -this.getPreferredWidth();
//
// setPadding(5,rightPadding,5,leftPadding);
// invalidate();
// }
CancelBooking()
{
heading = new LabelField(){
public void paint(Graphics g){
g.setColor(Color.WHITE);
super.paint(g);
}
};
heading.setFont(Font.getDefault().derive(Font.BOLD, Font.getDefault().getHeight()+2));
heading.setText("Check-In");
getMainManager().setBackground(
BackgroundFactory.createBitmapBackground(
Bitmap.getBitmapResource("PlainScreen.png")));
//setTitle("First Screen");
BasicEditField id=new BasicEditField("Last Name: ","");
BasicEditField name=new BasicEditField("Confirmation Number: ","");
//BasicEditField sal=new BasicEditField("Salary: ","");
add(id);
add(name);
//add(sal);
next=new ButtonField("Next");
next.setChangeListener(this);
// add(next);
}
public void fieldChanged(Field field, int context)
{
if(field==next)
UiApplication.getUiApplication().pushScreen(new MenuScreen());
}
}

You have to add FIELD_HCENTER and FIELD_VCENTER in the style of your field.
Consider reading this link
"Managers of Blackberry docs"

Related

Custom PopUp to work like tooltip without time out

I need to create a Customised PopUp to work like a ToolTip How can i achieve it
i have worked on a code please check out and tell me where i need improvement
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class ToolTip extends PopupScreen{
private static VerticalFieldManager vfm;
private LabelField lbl;
private int xPosition;
private int yPosition;
private String message;
public ToolTip(String message,int xPos,int yPos){
super(vfm);
this.xPosition=xPos;
this.yPosition=yPos;
this.message=message;
vfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0,0,getWidth(),getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0,0,getWidth(),getHeight());
super.paint(graphics);
}
};
lbl=new LabelField(message);
vfm.add(lbl);
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight());
setPosition(xPosition, yPosition);
}
}
i get an error of NullPointer Exception at super(vfm) because vfm is null when i use it as follows . How can i optimise my code .
ButtonField bf1=new ButtonField("Login"){
protected boolean navigationClick(int status, int time) {
UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50));
return super.navigationClick(status, time);
}
};
add(bf1);
Pass an initialized instance of VerticalFieldManager to super(..) of PopupScreen. Check following code.
class ToolTip extends PopupScreen {
private static VerticalFieldManager vfm = new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0, 0, getWidth(), getHeight());
super.paint(graphics);
}
};
// other codes
public ToolTip(String message, int xPos, int yPos) {
super(vfm);
// other codes
}
protected void sublayout(int width, int height) {
// other codes
}
}
Updated code for custom background support
class ToolTip extends PopupScreen {
private int xPosition;
private int yPosition;
public ToolTip(String message, int xPos, int yPos) {
super(new VerticalFieldManager());
// Define and set background here
// Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background
getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED));
// Add other UI Field here
this.xPosition=xPos;
this.yPosition=yPos;
add(new LabelField(message));
// other codes
}
// Empty implementation of this will disable default rounded border.
protected void applyTheme() {
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setPosition(xPosition, yPosition);
}
}
You need to use getDelegate().setBackground(..) to set the background. Check the class BackgroundFactory for creating custom background.

Editfield dynamic

I used the below code for edit text where I give the width and the height for the edit text and once the text fills the given height the field will scroll its content, but I want to make this code more dynamic. I want this editfield with only one line and it will grow for a number of lines done 3 or 4 and after this is done the field should scroll what it contains. Any help with that?
public class TextBoxField extends VerticalFieldManager {
private int managerWidth;
private int managerHeight;
private EditField editField;
public TextBoxField(int width, int height) {
super(Manager.NO_VERTICAL_SCROLL);
managerWidth = width;
managerHeight = height;
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
editField = new EditField(){
public void paint(Graphics g) {
getManager().invalidate();
super.paint(g);
}
};
vfm.add(editField);
add(vfm);
}
public void paint(Graphics g) {
super.paint(g);
g.drawRect(0, 0, getWidth(), getHeight());
}
public void sublayout(int width, int height) {
if (managerWidth == 0) {
managerWidth = width;
}
if (managerHeight == 0) {
managerHeight = height;
}
super.sublayout(managerWidth, managerHeight);
setExtent(managerWidth,managerHeight);
}
public String getText() {
return editField.getText();
}
public void setText(String text) {
editField.setText(text);
}
}

How can i create a popup for custom Choicefield and call it?

I have created my own simple ChoiceField by extending ObjectChoiceField.. It is like..
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Now i want to display the my choices when i select MyChoiceField in custom menu(not the default popup that blackberry provides when user clicks on a ChoiceField)..
How can i achieve it? Sample codes will be appreciable..
Thanks
Try Overriding the method fieldChangeNotify(int context) of ObjectChoiceField like this:
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
protected void fieldChangeNotify(int context) {
int index = getSelectedIndex();
try{
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));
PopupScreen popUpScreen = new PopupScreen(vfm){
public boolean onClose()
{
this.close();
return true;
}
};
UiApplication.getUiApplication().pushScreen(popUpScreen);
} catch(Exception exc) {
System.out.println("Exception in choiceField fieldChangeNotify");
}
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Use MyChoiceField now somewhat like this:
String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));
Let me know whether this solution works.

BlackBerry customized HorizontalFieldManager

In Blackberry I want to create a class that extends HorizontalFieldManager so that I can display a label and an image on the same line with the label to the left and the image to the right.
I want the user to be able to interact with the HorizontalFieldManager as if it were a single field. I also want each HorizontalFieldManager to be focusable when added to a VeriticalFieldManager. I also want the clcik action events.
Sounds like you'll be wanting to start writing your own Field classes, here's an example to get you started:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
public class SimpleField extends Field {
private String label;
private Bitmap image;
private int fieldWidth;
private int fieldHeight;
private boolean hover = false;
private int focusColor = 0xcccccc;
public SimpleField(String label, Bitmap image) {
super(Field.FOCUSABLE);
this.label = label;
this.image = image;
fieldWidth = Display.getWidth();
fieldHeight = image.getHeight();
}
protected void onFocus(int direction) {
hover = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
hover = false;
invalidate();
super.onUnfocus();
}
public int getPreferredWidth() {
return fieldWidth;
}
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int width, int height) {
setExtent(fieldWidth, fieldHeight);
}
protected void paint(Graphics graphics) {
if(hover){
graphics.setColor(focusColor);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
graphics.drawText(label, 0, (fieldHeight - graphics.getFont().getHeight()) / 2);
graphics.drawBitmap(graphics.getFont().getAdvance(label), 0, image.getWidth(), image.getHeight(), image, 0, 0);
}
}

Adding fields to a BlackBerry PopupScreen

I want to add fields and buttons to a BlackBerry PopupScreen.
You want customize the popup screen like this
public class CustomPopUpScreen extends PopupScreen {
public CustomPopUpScreen() {
super(new VerticalFieldManager(CustomPopUpScreen.NO_HORIZONTAL_SCROLL));
add(new ButtonField());
add(new ButtonField());
}
}
This code will help you
Here I create object of type PopupScreen and call the method givePopup:
PopupScreen popup1=givePopup();
PopupScreen givePopup(){
VerticalFieldManager popvfm =new VerticalFieldManager();
ButtonField ok=new ButtonField("ok");
ButtonField cancel=new ButtonField("cancel");
popvfm.add(ok);
popvfm.add(cancel);
PopupScreen popup=new PopupScreen(popvfm);
return popup;
}
This method returns a PopupScreen instance that has one vertical field manager and I add two button field on this manager.
public class FriendPopupScreen extends MainScreen{
int dialogWidth;
int dialogHeight;
LabelField lblTitle;
HorizontalFieldManager hfmTitle;
Vector data;
public FriendPopupScreen() {
dialogWidth = 300;
dialogHeight = 150;
lblTitle = newLabelField("Choose option");
hfmTitle = new HorizontalFieldManager(USE_ALL_WIDTH){
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
maxWidth= Display.getWidth();
maxHeight=40;
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics graphics) {
// TODO Auto-generated method stub
graphics.setBackgroundColor(Color.BLACK);
graphics.clear();
super.paint(graphics);
}
};
hfmTitle.add(lblTitle);
lblTitle.setMargin(10,0,0,10);
add(hfmTitle);
}
protected void sublayout( int width, int height ) {
setExtent( dialogWidth, dialogHeight );
setPosition( Display.getWidth()/2-(dialogWidth/2), Display.getHeight()/2 - (dialogHeight/2));
layoutDelegate( dialogWidth, dialogHeight );
}
}
and call this:
FriendPopupScreen popup = new FriendPopupScreen();
UiApplication.getUiApplication().pushModalScreen(popup);

Resources