Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:56+00:00 2026-06-14T21:59:56+00:00

I am currently working on a multi-line Edit Text which might have placeholders within

  • 0

I am currently working on a multi-line Edit Text which might have placeholders within its Text. To circumvent modifications of these placeholders I added an onClickListener to the EditText widget which checks if the cursor position is within such a placeholder. In that case the placeholder should be selected, to prevent any modifications except of completely deleting it.

This works perfectly fine on my Android 2.3 device, but on Android 4.x the selection is modified after the onClick event and the cursor shows up at the beginnning of the placeholder without the selection.

Below the source of the onClickListener.

protected void textClickListener(EditText v) { 
    Pattern p = Pattern.compile(placeholderRegex);
    Matcher matcher = p.matcher(v.getText());
    int sel_start = v.getSelectionStart();
    int sel_end = v.getSelectionEnd();
    if (sel_start == -1) {
        return;
    }
    while (matcher.find()) {
        int pattern_start = matcher.start();
        int pattern_end = pattern_start + 25;
        if (pattern_start > sel_end) {
            continue;
        }
        if (pattern_end < sel_start) {
            continue;
        }
        v.setSelection(Math.min(sel_start, pattern_start), Math.max(sel_end, pattern_end));
        return;
    }
}

This code works fine, the setSelection is called with the correct values and the selection is actually set. I set a breakpoint on the Selection.setSelection method and found out that it is called from PositionListener, which is an inner class of android.widget.editor and this sets the selection length to 0. Below is the stack trace of this setSelection call:

Selection.setSelection(Spannable, int) line: 87 
Editor$InsertionHandleView.updateSelection(int) line: 3271  
Editor$InsertionHandleView(Editor$HandleView).positionAtCursorOffset(int, boolean) line: 3045   
Editor$InsertionHandleView(Editor$HandleView).updatePosition(int, int, boolean, boolean) line: 3064 
Editor$PositionListener.onPreDraw() line: 2047  
ViewTreeObserver.dispatchOnPreDraw() line: 671  
ViewRootImpl.performTraversals() line: 1820 
ViewRootImpl.doTraversal() line: 1000   
ViewRootImpl$TraversalRunnable.run() line: 4214 
Choreographer$CallbackRecord.run(long) line: 725    
Choreographer.doCallbacks(int, long) line: 555  
Choreographer.doFrame(long, int) line: 525  
Choreographer$FrameDisplayEventReceiver.run() line: 711 
Handler.handleCallback(Message) line: 615   
Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92   
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4745    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 511  
ZygoteInit$MethodAndArgsCaller.run() line: 786  
ZygoteInit.main(String[]) line: 553 
NativeStart.main(String[]) line: not available [native method]

Any ideas how to prevent this or how to set the selection after this PositionListener action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:59:58+00:00Added an answer on June 14, 2026 at 9:59 pm

    Migrated From Question

    Finally I found out, that you have to modify the selection in an OnTouchListener. And to make it even more complicated, you have to modify the selection in both calls of that listener (ACTION_DOWN, ACTION_UP). If you just do it once (as I did for performance reasons) it won’t work either. So the finally working code is:

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    protected boolean textTouchListener(EditText v, MotionEvent event) {
        placeholder_selected = false;
        Pattern p = Pattern.compile(placeHolderRegex);
        Matcher matcher = p.matcher(v.getText());
        int click_position = v.getOffsetForPosition(event.getX(), event.getY());
        int sel_start = click_position;
        int sel_end = click_position;
        if (sel_start == -1) {
            return false;
        }
        while (matcher.find()) {
            int pattern_start = matcher.start();
            int pattern_end = pattern_start + 25;
            if (pattern_start > sel_end) {
                continue;
            }
            if (pattern_end < sel_start) {
                continue;
            }
            v.setSelection(Math.min(sel_start, pattern_start), Math.max(sel_end, pattern_end));
            placeholder_selected = true;
            return true;
        }
        return false;
    }
    

    The OnClickListener is still needed for compability reasons, because the method getOffsetForPosition is not available for API levels below 11 (Honeycomb). Depending on the used API, only one of both listeners has to be executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a project which supports multi-language. I have created a
I'm currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional
I have an input <input> When someone pastes multi-line text into the input, I
I'm currently working on a multi-step query form which can be found at: http://jsfiddle.net/xSkgH/47/
I'm currently working on a multi-step query form which can be found at: http://jsfiddle.net/xSkgH/47/
I'm currently working on a multi-threaded application, and I occasionally receive a concurrently modification
I am currently working on a multi branch desktop based project using VB.NET 2008.
Im currently working on a game that uses multi touch to apply zoom to
I'm currently working on a irc bot for a multi-lingual channel, and I'm encountering
I am currently working on a project where we have a couple very control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.