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: 91
Registered: ‎02-04-2009
My Device: Not Specified

Programmatically Selecting Contents of a BasicEditField

I have a BasicEditField on one of my Screens.  When I push the screen to the stack, the field is focused and the cursor is at position 0.  When the user types, the text is inserted before the contents of the field.  I would prefer that the entire contents of the BasicEditField are selected so that the existing text is replaced when the user begins typing.  Is this behavior possible?  I've looked at select(boolean) and setCursorPosition(int), but those aren't working for me.  Any ideas?  Thanks.
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

If I understood you correctly, You have a EditField and it Does contain the default values, when the use gets focus to teh Feild you would like to remove the Conent of EditField?

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

Almost.  I have a BasicEditField:

 

 

BasicEditField field = new BasicEditField("Name: ", "Enter your name...");

 

When I add that to the top of a screen and then push the screen, the field has focus.  When the field has focus, the cursor appears on the "E" in "Enter your name...."  If the user starts typing his name, the field will contain, for example, "John DoeEnter your name...."  To enter a name correctly, the user first has to delete the contents of the field using the delete/backspace key or choose Menu > Clear Field.  Another options is for the user to select the contents of the field (Menu > Select and then roll the trackball or hold Shift while rolling) and then start typing.  This has the effect of overwriting the contents, and it it what I would like to reproduce.

 

So, I would like to add my BasicEditField to a screen and then programmatically select the contents.  This will allow the user to overwrite the default contents by just starting to type. 

 

Thanks. 

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

One thing you can try is, Check Is the keyDown happening for the first time on that field, If it is first time clear your data.

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

Thanks for the suggestion.  That would work, but it might confuse the user because there would be no visual indication that typing would clear the field.  I think users are more used to a field clearing if it was highlighted when they started typing.
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

Other option is like, When the Field get focus then clear the Field.

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

The field is the first on the screen, so it always has focus by default when the screen is pushed.  The user would never see the original text.  Good suggestion though.
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

We could do some work around to make sure that the Edit Field will not be having the focus. We could set it to Some LabelField and then take the focus from there on.

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Programmatically Selecting Contents of a BasicEditField

create a named nullfield. add the nullfield at position 0 to the screen (it will get the focus).
in onFocus of the first editscreen check for the nullfield being displayed and remove it.
dirty hack, but works Smiley Happy
----------------------------------------------------------
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
Highlighted
Developer
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Programmatically Selecting Contents of a BasicEditField

Okay, that would technically work, but it doesn't work for me in this particular case Smiley Happy   How's this for a hack:

 

MainScreen screen = new MainScreen();

BasicEditField field = new BasicEditField("Name: ", "Enter your name...");

screen.add(field);

pushScreen(screen);

field.setCursorPosition(0); 

field.select(true);

EventInjector.invokeEventt(new EventInjector.NavigationEvent(EventInjector.NavigationEvent.NAVIGATION_MOVEMENT, 0, 1, 0));

 

setCursor(0) puts the cursor at the start of the field, select(true) part the editor in selection mode, and the EventInjector is used to simulate scrolling down, which has the effect of highlighting the entire field when combined with the cursor placement and select mode.  It's a hack for sure, but it actually works.  I'm not sure if I'll use it or not.

 

Thanks for the other suggestions.