How to get carret position from TextArea

12 views
Skip to first unread message

Mark Bolduc

unread,
Dec 16, 2019, 2:28:58 PM12/16/19
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA Netbeans 10
Desktop OS Windows 10 Pro
Simulator Latest
Device PC, IOS, Android

I have a populated TextArea that contains  formatted phone numbers (1-888-555-5555)

when the user touches a phone number (longPointerPress) I want to parse the textArea to find the "phone number"
In order for me to do that I need to know the index on the touch or pressed point.

the index I refer to is as in Swing an index refers to the carret position.

Thoughts?

Regards.

Shai Almog

unread,
Dec 16, 2019, 9:44:30 PM12/16/19
to CodenameOne Discussions
Is this text area editable?
If it's editable then that's a problem. A text area is a very special component that transitions between native and lightweight editing. As such you would be relying on native behavior and long press is already associated with functionality in native text editing.

If not then why use a text area?
Why not have multiple buttons in flow layout?
If you want to give users the ability to edit you can just dynamically replace one of them with a text field. You can style the whole thing to look like one big text area if you want to or you can create a unique UX of your own.

Mark Bolduc

unread,
Dec 17, 2019, 8:09:02 AM12/17/19
to CodenameOne Discussions
No

        txt.setEditable(false);
        txt.setEnabled(true);
        txt.getStyle().setAlignment(LEFT);
        txt.setSingleLineTextArea(false);

User chooses Edit button to change the txt.setEditable(true);

The behaviour I am looking for is when the TextArea is not editable.

I would think the behaviour I am looking would be common, other than a series of Buttons, is there another Object that can contain Text and provide me an index into the text when touched?

Thoughts?

Regards

Shai Almog

unread,
Dec 17, 2019, 9:05:51 PM12/17/19
to CodenameOne Discussions
Editing is a difficult process because we transition from lightweight to heavyweight so text might not be in the exact same position. In the lightweight you might click on a word which will be a different word in the native. As I said, native editing already does things like long press and since styled editing is problematic in all native OS's it just isn't supported.

You can use HTML and browser component. HTML is generally really good for text related work. Native apps take that approach often. This seems like a task that makes some sense to HTML.

Or you could just switch components when going to edit mode using replace. You could use something like this during view and a text area during edit: http://www.codenameone.com/blog/tip-lightweight-rich-text-view.html


Reply all
Reply to author
Forward
0 new messages