Question

Trying to reduce the height of a DateField from default 53 to 32, but to no avail. The height stays the same with the result that the text appears cut out towards the bottom of the background which is smaller in height (32).

I tried 2 ways:

    DateField dtf = new DateField(label, defaultDateTime, DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY )
    {
        protected void layout(int width, int height)
        {
            Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, true);

            width = bmp.getWidth();
            height = bmp.getHeight();
            super.setExtent(width, height);

            super.layout(width, height);
        }           
    };

2nd method (based on a Stack Overflow post):

public class MyDateManager extends VerticalFieldManager {
    protected DateField dateField_ = null;
    public MyDateManager(String label, long defaultDateTime) {
        super(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
        dateField_ = new DateField(label, defaultDateTime, DateField.DATE_TIME |
            Field.FIELD_HCENTER | DrawStyle.HCENTER | Field.FOCUSABLE | Field.READONLY);

        this.add(dateField_);
    }

    protected void sublayout(int width, int height) {
        Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, false);

        width = bmp.getWidth();
        height = bmp.getHeight();

        layoutChild(dateField_, width, height);
        dateField_.setBackground(BackgroundFactory.createBitmapBackground(bmp, 0, 0, Background.REPEAT_NONE));
        int h = dateField_.getHeight();
        setExtent(width, height);
    }
 }

Please suggest.

Thanks

Was it helpful?

Solution

Second method is a horrible approach (messing with VerticalFieldManager's height will get you a lot of problems).

Try the first method again, but this time call super.layout first:

    DateField dtf = new DateField(label, defaultDateTime, DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY )
    {
        protected void layout(int width, int height) {
            super.layout(width, height);

            Bitmap bmp = UIElements.GetBitmap(UIElements.IMG_DROPDOWN_BG, true);

            width = bmp.getWidth();
            height = bmp.getHeight();
            super.setExtent(width, height);
        }           
    };

Notice that using this approach you might also get glitches, as you are blindly overriding a method without considering the rest of the class. To do this safely you would need to have access to the parent class source, and you don't.

I'd suggest to extend HorizontalFieldManager (override sublayout) to have a fixed height (Never VFM because the scrolling goes in the same direction you are trying to make fixed). Then place inside a regular date field that uses all the available height (pass the flag Field.USE_ALL_HEIGHT in the constructor).

More info: http://docs.blackberry.com/es-es/developers/deliverables/29251/Determining_the_dimensions_of_a_field_1578864_11.jsp

OTHER TIPS

I suspect this is a battle you are likely to loose.

Personally I would create a DateField look-a-like from a LabelField which will give you the restrictions that you need.

DateField is a specialised Field and in my testing, it will ignore the limitations you put upon it, regardless of whether you try to do this via the containing Manager or directly in the Field's layout method. It seems the actual height it will choose for itself will vary by device.

I suspect on all touchscreen capable devices, it will try to give itself enough height to be touched. In terms of pixels that will depend on the resolution (dpi) of the device, as in fact your image should, because it will look different on a high resolution device, like the 9900 to a low resolution device, like the 9300.

I suspect on non-touchscreen devices, it will probably follow the font size specified.

I tested the following code:

    DateField dtf1 = new DateField("test1", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE | Field.READONLY ) {
        protected void layout(int width, int height) {
            super.layout(width, 32);
            System.out.println("height1: " + Integer.toString(this.getHeight()));
        }           
    };
    DateField dtf2 = new DateField("test2", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE ) {
        protected void layout(int width, int height) {
            super.layout(width, 32);
            System.out.println("height2: " + Integer.toString(this.getHeight()));
        }           
    };
    DateField dtf3 = new DateField("test3", System.currentTimeMillis(), DateField.DATE_TIME | DrawStyle.LEFT | DrawStyle.TOP | Field.FOCUSABLE ) {
        protected void layout(int width, int height) {
            super.layout(width, 32);
            System.out.println("height3: " + Integer.toString(this.getHeight()));
        }           
    };

...

        add(dtf1);
        add(dtf2);
        Font smallFont = this.getFont().derive(Font.PLAIN, 15);
        dtf3.setFont(smallFont);
        add(dtf3);

On a 9800, the DateField was always 40 pixels in height. On a 9300 is was 18 for the first 2 and 15 for the third.

So, since you are using this DateField just to display a non updateable value, I recommend instead you look at using something like SimpleDateFormat, to format the date as you want, and display this in a LabelField. You can then manage the height of the Field using the Font you specify.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top