Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 125
Registered: ‎08-11-2011
My Device: Blackberry Smartphone 9780
My Carrier: -

Correctly aligned of DateField

I have a CustomHorizontalFieldManager that override getPreferredWidth and Height, and sublayout with setExtent... and I have painted a border around the manager.


Well, I tried a lot of way for align correctly a datefield inside this custom manager, but no work with all size fonts...


I.E. I have added the datefield inside a custom absolute field manager, with the function addField(field, x, y) ...


x = 2 ;

y = manager.getPreferredHeight()/2 - datefield.getFont.getHeight()/2 ;


This works when the font is I.E. ,7,8,9 (units), but when the font is less don't works (the datefield is not correctly aligned)


Obviously I tried diferrents ways, like use HorizontalFieldManager.FIELD_VCENTER... or another styles...


In other words, I trying to align a datefield (the text in the datefield) vertically inside a horizontal manager...


I need use the same solution for different devices, like 9780 & 9800.


some suggestion?





My good english is beceause i dont speak english
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Correctly aligned of DateField

Posts: 125
Registered: ‎08-11-2011
My Device: Blackberry Smartphone 9780
My Carrier: -

Re: Correctly aligned of DateField

[ Edited ]

I tried all without success.


What I need :



(The png has some errors, dont worry about that)


What I have :




package mypackage;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public final class MyScreen extends MainScreen {

	public MyScreen() {

		DateField df = new DateField(null, System.currentTimeMillis(), DateField.DATE);
		setFontSize(df, 5);
		CustomAbsoluteFieldManager afm = new CustomAbsoluteFieldManager(240, 40);
		DateField df2 = new DateField(null, System.currentTimeMillis(), DateField.DATE);
		setFontSize(df2, 6);
		CustomAbsoluteFieldManager afm2 = new CustomAbsoluteFieldManager(240, 40);
		DateField df3 = new DateField(null, System.currentTimeMillis(), DateField.DATE);
		setFontSize(df3, 7);
		CustomAbsoluteFieldManager afm3 = new CustomAbsoluteFieldManager(240, 60);
		int x = 10;
		int y = afm.getPreferredHeight()/2 - df.getFont().getHeight()/2;
		int y2 = afm.getPreferredHeight()/2 - df2.getFont().getHeight()/2;
		int y3 = afm.getPreferredHeight()/2 - df3.getFont().getHeight()/2;

	public void setFontSize(Field f, int size) {
		Font font = f.getFont();
		font = Font.getDefault().derive(font.getStyle(), size, Ui.UNITS_pt);


package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.AbsoluteFieldManager;

public class CustomAbsoluteFieldManager extends AbsoluteFieldManager {

	Bitmap picture = Bitmap.getBitmapResource("img/calendar.png");
	boolean _paintBorder = false;
	int _width, _height = 0;

	public CustomAbsoluteFieldManager(int width, int height) {
		_width = width;
		_height = height;

	public int getPreferredWidth() {
		return _width;

	public int getPreferredHeight() {
		return _height;

	public void paintBorder(boolean paint) {
		_paintBorder = paint;
	protected void sublayout(int maxWidth, int maxHeight) {
		super.sublayout(_width, _height);
		super.setExtent(_width, _height);
	protected void paint(Graphics g) {
		if (_paintBorder) {
			g.drawRoundRect(0, 0, _width, _height, 8, 8);
		g.drawBitmap(_width - picture.getWidth() - picture.getWidth() / 2, _height / 2 - picture.getHeight() / 2, _width, _height, picture,
				0, 0);



I already tried with Horizontal and Vertical managers... overriding sublayout...


Thanks anyway for your help.


My problem is that there is no point in DateField that help me to calculate the Y coord in the manager... because font height doesn't work, contentRect.height does not work, getExtent neither.. when I said dont work I trying to said that I get erroneous Y coords.


I need the date field, just like the calendar image.


My good english is beceause i dont speak english