0

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

Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67
  • the problem is with this.setExtent(contactButtonWidth, HARD_CODED_HEIGHT);. you change this line, so you will get the correct position – Rince Thomas Mar 27 '14 at 11:59
  • I have used setExtent to set the height and width of the button. I need button of full screen width. – Sam-In-TechValens Mar 27 '14 at 12:39
  • Try passing in the dimensions you want to layout, i.e. super.layout(contactButtonWidth, HARD_CODED_HEIGHT);. In general, issuing setExtent() after invoking layout() is bad practise and unlikely to achieve the results you want (expect a few cases). So if passing in the size you want (and you should use USE_ALL_HEIGHT too) does not work, then you are up for overriding paint(). – Peter Strange Mar 27 '14 at 13:04
  • @Peter Strange: Still I am getting the text in Right Corner, please see the code I am using according to your suggestion – Sam-In-TechValens Mar 28 '14 at 06:23

2 Answers2

1

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);
    }
};
Peter Strange
  • 2,640
  • 11
  • 11
0

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();
    }
};