EditText's cursor position

Assuming, that a user has written some text into an EditText and thereafter touched somewhere else on the screen, which caused the cursor position to change: How can one determine the new cursor position?

-------------Problems Reply------------

The simple version:


If you want to react on an event you may try

myEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// view is myEditText here

event allows to distinguish between presses and releases.

EditText also has a setOnClickListener() that might be worth to look at.

EDIT: I forgot to mention onSelectionChanged(int selStart, int selEnd) where selEnd equals selStart if the position changed.

Best and safe way is using TextWatcher

public void onTextChanged(CharSequence s, int start, int before, int count) {
int cursorIndex = start + 1;

Category:android Views:1 Time:2010-08-10

Related post

  • Android EditText's cursor position tracking when clicking or toching by first time itself 2011-04-29

    i am struggling with EditText for morethan a day. My goal is to know current cursor positon that user's first click on the EditText. Assume current cursor at position "5", Click EditText widget at position "8", At this moment getSelectionStart() retr

  • Android EditText's cursor coordinates (absolute position) 2011-02-08

    How can I get the coordinates of the cursor in the EditText ? I am not trying to get the cursor position here but the EditText coordinates of the cursor. In my case, when I send a number via KeyEvent to the EditText, the cursor position (getSelection

  • Android: EditText listener for cursor position change 2010-09-06

    I'm looking for a way to detect a cursor position changed in an EditText. I couldn't find anything in the documentation so far. Has anyone solved this already? --------------Solutions------------- You can override onSelectionChanged (int selStart, in

  • How to get cursor position (x,y) in EditText android 2011-02-18

    I have ploblem:get cursor position x,y in EditText android? You can help me? --------------Solutions------------- int pos = editText.getSelectionStart(); Layout layout = editText.getLayout(); int line = layout.getLineForOffset(pos); int baseline = la

  • How to set Cursor position in EditText? 2011-11-07

    There are two EditText,while loading the page a text is set in the first EditText ,so cursor will be now in the starting place of EditText ,I want to set cursor position in second EditText which contains no data.How to do this? --------------Solution

  • How to set cursor position in softkeyboard(not in EditText or TextView!!!) in Android? 2011-12-11

    The following code works for only 1000 characters. If there are more than 1000 characters, the cursor will jump back to the end of 999 characters and start working again. This is because getTextBeforeCursor(n,flag) can offer only the n number of char

  • Clearing the entire text before cursor position on EditText 2012-01-18

    I want to clear the entire text on EditText which is before the cursor position. Suppose i the text is 1234567890, the cursor is after the character 4 like this 1234|567890 Now my requirement is I have a custom button, which deletes the text before c

  • Android: Disallow user to select cursor position in EditText? 2012-02-22

    Right now I use onFocusListener to automatically move the cursor to the end of the EditText string. This works fine when the box is being Focused, however, once focused if the user taps a position in the string, the cursor moves there. I've tried imp

  • How to get cursor position in a Edittext 2012-03-21

    I am making an Android application. In my application, I have to find the cursor position of a edittext. For example, I put the text "how are you" on edittext. If I click next to character 'a' then, I have to find the cursor now in front of 'a'. How

  • Edittext line number and currentline cursor position. 2012-03-21

    Now I am working on a Android application. I created a custom keyboard with functionalities. I am using a edittext for displaying the entered texts. Edit text may have n number of lines.Now my problem is I have a up button in my keyboard.So if I clic

  • In Android EditText how to get the cursor position in an OnTouchListener after it is set 2012-04-03

    I am building a text editing component that I need to capture the character position after the user touches the screen. I have added an OnTouchListener, however what I am finding is that I am getting the original character position, before the user h

  • EditText's cursor coordinates 2010-10-23

    As the title suggests, how can i get the coordinates of the cursor in the EditText ? im not trying to get the cursor position here but the screen coordinates of the cursor. im trying to show a popup relative to that cursor, therefore i need to determ

  • How to close soft keyboard and have a possibility to determine cursor position 2011-01-30

    How to close soft keyboard and have a possibility to determine cursor position by touching place in EditText? InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(myEditText.getWindow

  • Get Cursor Position in Android in Edit Text? 2011-08-01

    I am using a custom EditText View. I have overridden the OnKeyUp event and am able to capture the Enter Key press. Now my requirement is, when the user has entered a text "Hi. How are you?" and then keeps the cursor after the word "are" and press ent

  • Insert character between the cursor position in edit text 2011-10-05

    My code is : EditText edt edt.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { final String number = edt.getText().toString(); int count = arg0.length(); edt.setSelection(count); } } @Override public

  • Edittext set cursor to next line 2013-06-17

    I am working on custom keyboard where i have one problem. On key listener I need to set my current cursor position to next line. I have tried using editText.setSelection(editText.getText().length()); which sets the current cursor position to end of t

  • Display DIV at Cursor Position in Textarea 2008-09-24

    This question already has an answer here: How do I get the (x, y) pixel coordinates of the caret in text boxes? 4 answers For a project of mine I would love to provide auto completion for a specific textarea. Similar to how intellisense/omnicomplete

  • Current Cursor Position when Using the Prawn Ruby Library 2008-10-08

    I'm using the Prawn Ruby library (http://prawn.majesticseacreature.com/) to generate some pdf documents. I draw a table without any problem. Next, I want to insert some lines after the table for various people's signatures. Before I draw the lines, I

  • Getting cursor position in a Textarea 2008-12-04

    I am trying to implement Autocomplete in a text area (similar to http://www.pengoworks.com/workshop/jquery/autocomplete.htm). What I am trying to do is when a user enters a specific set of characters (say insert:) they will get an AJAX filled div wit

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)