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
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified
Accepted Solution

custom editField and passwordEditField

Hello,

 

I am using an editField and a passwordEditField with a rounded rectangle as background.

The codes are this:

 

EditField myEditField = new EditField(){
     public void paint(Graphics g) {
	int prevColor = g.getColor();
	g.setColor(0xffffff);
	g.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
	g.setColor(prevColor);
	super.paint(g);
    }
};
PasswordEditField myPasswordEditField = new PasswordEditField(){
     protected void paint(Graphics g) {
	int prevColor = g.getColor();
	g.setColor( 0xffffff);
	g.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
	g.setColor(prevColor);
	super.paint(g);
     }
};

 

They both look ok, but problem is when user writes text it is placed too close to the rectangle border, as I show in next picture:

 

Dibujo.JPG

 

I tried to move the text to the right replacing

super.paint(g); line by 

g.drawText(getText(), 5, 0);

in the overwrited method.

 

But while user is writing last letter looks like cutted in half, second line does not appears and on passwordEditField characters are not replaced by asterisks.

 

 

Thanks in advance.

Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: custom editField and passwordEditField

Your picture is not working.

What you can try is to setPadding around the text

setPadding(3,3,3,3) for example. Its not documented in the API, some people say be cautious when using it because it could be removed or changed at anytime I guess.

I am using this for mine however.
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: custom editField and passwordEditField

Thanks for your answer, I already tried with myEditField.setPadding(3,3,3,3), but it only cut rectangle border and text still appears too close to rectangle left border...

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: custom editField and passwordEditField

[ Edited ]

For some reason, text fields do not setPadding well. I would add the field to a manager (HorizontalFieldManager, for example) and set border and padding on the manager.


By the way - setPadding indeed was working but undocumented on many OS versions, starting with at least 4.2. They finally documented it in OS 6.0 and it works the same way (where it works).

 

[Edit]: if you add the field to a manager, setMargin on the field (instead of setPadding on the manager) should also work.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: custom editField and passwordEditField

[ Edited ]

 

 


arkadyz wrote:

For some reason, text fields do not setPadding well. I would add the field to a manager (HorizontalFieldManager, for example) and set border and padding on the manager.


By the way - setPadding indeed was working but undocumented on many OS versions, starting with at least 4.2. They finally documented it in OS 6.0 and it works the same way (where it works).

 

[Edit]: if you add the field to a manager, setMargin on the field (instead of setPadding on the manager) should also work.


Ahh thank you, I never though of putting it in a manger and setting a padding on it. I was having a debate whether I should use setborder on my editfields to space them out more but then I would lose the actual border I wanted.

 

BTW is there a way (for example) if your using a custom edit field class to override something and automatically add it to a manager and setPadding on the manager rather than declare all around each editfield a new manager with padding code. 

 

At OP.

 

Dont know what you mean, here are my results with setPadding and without.

 

With setPadding(3,3,3,3);

Without

 

Is this the problem you are having?

 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: custom editField and passwordEditField

create a custom manager class with this functionality and reuse it in your projects.
using the same approach as arkadyz btw
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: custom editField and passwordEditField

why you don't change the width in editField

 

public int getPreferredWidth()
{
//set width using text width
return (this.getFont().getAdvance(DEFAULT_TEXT)+20); }
protected void layout(int width, int height) { width = getPreferredWidth(); super.layout(width, height); }

 

correct me if i'm wrong.. ^^

 

Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: custom editField and passwordEditField

thanks for your help.
I can not set borders because I use o.s 4.5.
Highlighted
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: custom editField and passwordEditField

Thanks all for your help,

I filled the rectangle on the manager and put the field on it.