Question

I am an an Android Developer, developing an Blackberry application.

I have created a button of full width of screen. Getting problem in shifting the text to the center of the button area.

Used below code :

ButtonField _contactButton = new ButtonField(Constants.contactButtonTitle,Field.FIELD_HCENTER|Field.USE_ALL_WIDTH |
                Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
            protected void layout(int width, int height) {
                super.layout(width, height);
                HARD_CODED_HEIGHT = this.getHeight()/2 + 6;
                this.setExtent(contactButtonWidth, HARD_CODED_HEIGHT);
            }
            public int getPreferredWidth() {
                return contactButtonWidth;
            }
        }; 

enter image description here

Now using the below code :

ButtonField _contactButton = new ButtonField(Constants.contactButtonTitle,Field.FIELD_VCENTER|Field.USE_ALL_WIDTH |
                Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
            protected void layout(int width, int height) {
                super.layout(getPreferredWidth(), height);
            }

            public int getPreferredWidth() {
                return (Display.getWidth()-60);
            }
        };

Still getting the issue .. text of my button align to right corner. Please suggest

Was it helpful?

Solution

ButtonField appears to be a little 'broken'. But it also appears to be consistently broken in all the OS Levels that I have tested (OS 5.0 to OS 7.1), so I think we can achieve what you want by working round the broken bits and be confident the workaround will work in all levels you want.

As has been noted, ButtonField ignores USE_ALL_WIDTH, but does respect preferredWidth. So if you want to set the width of your ButtonField, then just override getPreferredWidth(). You should not do anything with width in layout.

Now you are using the styles for ButtonField already. Given that we have discarded USE_ALL_WIDTH as a useful style, I note that you also use FIELD_HCENTER. You should be aware that this is actually a directive to the Manager that is positioning this Field - telling the Manager to position the Field in the centre of the width the Manager has available. This style does not relate to how the contents of the ButtonField are drawn.

For that, you can look to use DrawStyle. By default, ButtonField uses DrawStyle.RIGHT. And it respects DrawStyle.Left - the text will be drawn on the left. It does not however, respect DrawStyle.HCENTER. So to get centred text, we need to paint the text ourselves.

There is one more complication. ButtonField passes a Context area into its paint() method, not the full Field canvas - presumably it does not pass in the edges because these are painted by a border. So to centre the text appropriately, we have to use the clipping region that has been passed in.

Here is the final, hopefully working, ButtonField. I appreciate you will have to spend some time creating a class for this, I'm sorry, I've been lazy and done in it 'in-line'. Please publish your CenteredTextButtonField class if you create one....

final String buttonText = "Test";
ButtonField _contactButton = new ButtonField(" ",
        Field.ACTION_INVOKE | Field.FOCUSABLE | ButtonField.CONSUME_CLICK){
    public int getPreferredWidth() {
        return contactButtonWidth;
    }
    protected void paint(Graphics graphics) {
        super.paint(graphics);
        XYRect clippingRect = graphics.getClippingRect();
        int centreY = (clippingRect.height - graphics.getFont().getHeight()) / 2;
        graphics.drawText(buttonText, 0, centreY, DrawStyle.HCENTER, clippingRect.width);
    }
};

OTHER TIPS

USE_ALL_WIDTH is our instruction to the field. Surprisingly, ButtonField ignores such instructions. Even more surprisingly, it honors its own getPreferredWidth (as illogical as it sounds). So drop that USE_ALL_WIDTH and define your ButtonField like this:

ButtonField testBtn = new ButtonField("Button") {
    public int getPreferredWidth() {
        return Display.getWidth();
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top