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
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930
Accepted Solution

Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

[ Edited ]

I have a Basiceditfield with initial text. Following is the code: 

searchField = new EditField("","",1024,EditField.FILTER_DEFAULT|EditField.NO_NEWLINE)
{
protected void onFocus(int direction)
{
setLabel("");
this.invalidate();
};
protected void onUnfocus()
{
setLabel("Search");
this.invalidate();
};
};
searchField.setLabel("Search");

Initialy text in searchfield is"Search". Onfocus its set to "". I type something in searchfield ,suppose  "ab".When focus is removed from searchfield label is set to "Searchab". whereas i want it as "Search". Please help.

Developer
Dionysus
Posts: 152
Registered: ‎07-28-2011
My Device: Bold 9900

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

in onUnfocus, first clear the text before setting the label.

 

setText("");

setLabel("Search");

 

 

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

[ Edited ]

I want following things to be done:
1.When i do not type anything in searchfield and if i focus and unfocus searchfield, then Initially text should be "Search". Onfocus text should be blank. Onunfocus text should be "Search".
2.When i type text in searchfield, and remove focus then text in editfield should be typed text only.
3.After condition 2 ,when i focus editfield and deleted all the text which i have type and remove the focus then text should b set to "Search". And searching should not get start with "Search".
Please help me out.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

on focus, use getText, check if it is "Search", setText to ""
on onfocus, use getText, check if it is "", set it to "Search".
on keyChar, use getText, check if is "Search, use setText to "", then return super.
----------------------------------------------------------
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
sajit56
Posts: 98
Registered: ‎08-17-2010
My Device: BlackBerry 8300 simulator

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

You may try to override paint method as per your need (see sample below). 

protected void paint(Graphics graphics) {
super.paint(graphics);
if (!hasFocus) {
if (editField.getText() == null	|| editField.getText().equals("")
|| editField.getText().trim().length() == 0) {
editField.setText("");
// if EditField is Empty Then it will show initial String
graphics.setColor(Color.GRAY);
int y = (height - editField.getFont().getHeight()) / 2;
graphics.drawText(initial, 10, y + 1);
graphics.setColor(Color.BLACK);
	                                      }
			}
Regards,
Ajit Sharma
Developer
sajit56
Posts: 98
Registered: ‎08-17-2010
My Device: BlackBerry 8300 simulator

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

I am agree with 

Regards,
Ajit Sharma
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

[ Edited ]

simon hain:
I did the changes as per your suggestion:

searchField = new EditField("","",1024,EditField.FILTER_DEFAULT|EditField.NO_NEWLINE)
{
protected void onFocus(int direction)
{
if(getText() == "Search")
setText("");
this.invalidate();
};
protected void onUnfocus()
{
if(getText() == "")
setText("Search");
this.invalidate();
};
protected boolean keyChar(char key, int status, int time)
{
if(this.getText().equals("Search"))
this.setText("");
return super.keyChar(key, status, time);
}
};
searchField.setText("Search");

 

I am facing following problems:

a.Unable to set text to "" on focus.

b.Unable to do thing specified in condition 3 as i mentioned above.

c.When i click on searchfield it is not set to "".

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: Problem with keeping initial text of basiceditfield same on unfocus after typing the text.

OHH my mistake.....condition should be getText().equals("Search")
and not getText() == "Search". Thanx simon. working fine.