Welcome!

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

Reply
New Developer
mistapony
Posts: 29
Registered: ‎11-09-2008
My Device: Not Specified

Substitute for setPadding for a TextField

I have a text field that uses an image for the background.  My problem is that without using the undocumented field.setPadding() method I am unable to position the label and cursor in the correct place.  Can anyone suggest a better way to do this because I need to be able to build this application for 4.5

 

 

public ImageTextField(String text, String initVal, String imageType) { super(text, initVal); font = getFont(); label = text; ImageSelector imageSel = new ImageSelector(imageType); offFocus = imageSel.getOffFocusBitmap(); onFocus = imageSel.getOnFocusBitmap(); currentPicture = offFocus;

setPadding(getTopTextPadding(), 0, 0, getLeftTextPadding()); } /** * Field implementation. * @see net.rim.device.api.ui.Field#getPreferredHeight() */ public int getPreferredHeight() { return currentPicture.getHeight(); } /** * Field implementation. * @see net.rim.device.api.ui.Field#getPreferredWidth() */ public int getPreferredWidth() { return currentPicture.getWidth(); } /** * Field implementation. * @see net.rim.device.api.ui.Field#layout(int, int) */ protected void layout(int width, int height) { super.layout(width, height); setExtent(Display.getWidth(), currentPicture.getHeight()); } /** * Field implementation. * @see net.rim.device.api.ui.Field#paint(Graphics) */ protected void paintBackground(Graphics graphics) { graphics.drawBitmap(getLeftTextPadding(), 0, currentPicture.getWidth(), currentPicture.getHeight(), currentPicture, 0, 0); } public int getLeftTextPadding(){ int hres = Display.getWidth(); int ltPadding = (hres - currentPicture.getWidth()) / 2;

return ltPadding; } public int getTopTextPadding(){ int topPadding = currentPicture.getHeight() / 2; return topPadding; }

 

Thanks for the help.

 

 

Please use plain text.
Developer
Developer
rab
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: Substitute for setPadding for a TextField

If you want to specify where to draw and include the padding in your calculation, do not use:

setPadding(....).

When you use setPadding() and you draw a bitmap at (0,0), the coordinate you specify are set after the padding is set by the field. So if the field starts at (100,100) on the screen, and you set a padding of left = 10 using field.setPadding(..), so when you draw your bitmap at (0,0) in the field, it will not start at 100, but at 110 on the screen.

 

I hope that this will help.

 

 

Please use plain text.
New Developer
mistapony
Posts: 29
Registered: ‎11-09-2008
My Device: Not Specified

Re: Substitute for setPadding for a TextField

Hmm,

 

Well set padding seems to work as I want.  I draw a bitmap in the paintBackground(Graphics g) method that  works well for my background image.  I tried to also do graphics.drawText and draw the label and editable field in that method also.  The problem is it does not seem to draw the label or the editfield in the correct place.  setPadding ends up putting the label and editfield in the correct location.  What is the best way to go about this?  I tried overriding the layout method but then the label and editable field never seems to get drawn.

Please use plain text.
New Developer
mistapony
Posts: 29
Registered: ‎11-09-2008
My Device: Not Specified

Re: Substitute for setPadding for a TextField

Has anyone else created an image for a background of an edit or textField? This is driving me crazy I can't seem to be able to get it right. I can't see to position the label or cursor in the middle of my image field. Any pointers for this?

Thanks again.
Please use plain text.