BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-25-2008, 04:29 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: AT
Posts: 81
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default drawing Text at the Center

Please Login to Remove!

Hi,

I have to display the number of button fields dynamically on the screen.
I have been drawing the button with the following code but my text doesn't fit on the buttons.

public class BigButtonField extends Field {

private int backgroundColour = 0xFFFFFF;
private Bitmap button;
private Bitmap on = Bitmap.getBitmapResource("onbutton.png");
private Bitmap off = Bitmap.getBitmapResource("offbutton.png");
private int fieldWidth = Graphics.getScreenWidth()-214;
private int fieldHeight = 34;
private int buffer = (Graphics.getScreenWidth() - 105) / 2;
private String text;
private Font fieldFont;
private Vector blackBerryContacts;
public int _contacts;;
private int _size;
private boolean status;

public BigButtonField(String _text) {
super(Field.FOCUSABLE);
text = _text;
button = off;

fieldFont = FieldFont();

}

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}

protected void onFocus(int direction) {
button = on;
status =true;
invalidate();
}

protected void onUnfocus() {
button = off;
status =false;
invalidate();

}

public int getPreferredWidth() {
return fieldWidth;
}

public int getPreferredHeight() {
return fieldHeight;
}

protected void layout(int arg0, int arg1) {
setExtent(getPreferredWidth(), getPreferredHeight());
}

public static Font FieldFont() {
try {
FontFamily theFam = FontFamily.forName("SYSTEM");
return theFam.getFont(net.rim.device.api.ui.Font.BOLD, 14);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}

protected void drawFocus(Graphics graphics, boolean on) {
//
}

protected void fieldChangeNotify(int context) {
try {
this.getChangeListener().fieldChanged(this, context);
//FlowFieldManager fm =new FlowFieldManager();


} catch (Exception exception) {
}
}




protected void paint(Graphics graphics) {

// Dialog.inform("total contacts ="+_contacts);
graphics.fillRect(0, 0,fieldWidth-120, fieldHeight);
//for(start=0;start<_contacts;start++)
//{
graphics.setBackgroundColor(0xfafafa);
graphics.drawBitmap(2, 0,fieldWidth, fieldHeight,button, 0, 0);
if(status==true)
graphics.setColor(0x000000);
if(status ==false)
graphics.setColor(0xffffff);
graphics.setFont(fieldFont);

//((fieldWidth - fieldFont.getAdvance(text))/2)-105

//if(text.length()<=3)
//graphics.drawText(text,buffer-80,(fieldHeight-fieldFont.getHeight())/2);
if(text.length()<=7)
graphics.drawText(text,buffer-75,(fieldHeight-fieldFont.getHeight())/2);
//if(text.length()==5)
//graphics.drawText(text,buffer-75,(fieldHeight-fieldFont.getHeight())/2);
if(text.length()>7)
graphics.drawText(text,buffer-90,(fieldHeight-fieldFont.getHeight())/2);

}
}
Offline  
Old 10-25-2008, 04:29 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: AT
Posts: 81
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any idea?
Offline  
Old 10-26-2008, 08:01 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 8130
PIN: N/A
Carrier: US Cellular
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, it's really weird for you to name the variable that is your horizontal coordinate "buffer".

Why don't you have drawText center it horizontally for you?
Code:
graphics.drawText(text, 0, (fieldHeight-fieldFont.getHeight())/2, DrawStyle.HCENTER|DrawStyle.HALIGN_MASK, getWidth());
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.