Android Edittext Listener for Cursor Position Change

Android EditText listener for cursor position change

Just subclass or extend the class EditText and add the following code to the newly create class:

 @Override 
protected void onSelectionChanged(int selStart, int selEnd) {
// Do ur task here.
}

Don't forget to add constructors to the subclass. :)

Android: EditText listener for cursor position change

You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as well (in this case selStart == selEnd)

How to have fixed cursor position for EditText?

You can apply this trick, This worked for me.
Just disable the cursor from edittext and on its click listener add your code that will keep the cursor on right most.

Here is code that i tried.

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="12"
android:background="@color/white"
android:padding="@dimen/activity_margin_10"
/>

Java Code

EditText mEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

mEditText = (EditText)findViewById(R.id.edit); // Initialzation of Edittext
mEditText.setSelection(mEditText.getText().length()); // After initialization keep cursor on right side
mEditText.setCursorVisible(false); // Disable the cursor.

/*
Add Click listener and on put your code that will keep cursor on right side
*/
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setSelection(mEditText.getText().length());
}
});
}

Hope it will help you.

EditText's cursor position

The simple version:

myEditText.getSelectionStart();

If you want to react on an event you may try

myEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
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.

Android Edit Text Cursor position doesn't change in first time

If I keep it simple, the following snippet is sufficiently enough to achieve what you want (apparently)

final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(text1 + text2);
// Keep it simple for the selection, it will place the cursor at end of text1
editText.setSelection(text1.length());
// editText.setCursorVisible(true); // I don't think it is required

// You don't need to go for OnTouchListener, OnClickListener will do for you
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setSelection(editText.getText().length() - text2.length());
}
});

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable editable) {
if (!editable.toString().contains(text1) || !editable.toString().contains(text2)) {
editText.setText(text1 + text2);
editText.setSelection(text1.length());
}
}
});

UPDATE 1

The above code works fine if it is the first EditText to gain focus, otherwise, if soft keyboard is already open, the cursor at first is shown at tapped place. Here's a work around for this (not the best solution, but it will work for you)

editText.setCursorVisible(false);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
editText.post(new Runnable() {
@Override
public void run() {
editText.setSelection(editText.getText().length() - text2.length());
editText.setCursorVisible(true);
}
});
} else {
editText.setCursorVisible(false);
}
}
});

How do I get the current cursor on Edittext Android?

You can use "OnClickListener" to get cursor position. Because "onTouch" is called before the cursor position changes.

text.setOnClickListener {
val start = text.getSelectionStart()
val end = text.getSelectionEnd()
Log.e("Log", "====>" + start + " " + end)
}

set cursor position of edittext in center...!

I found now what is main problem...
Problem is in the API level..

I change API level of my program.

Below API 15, cursor is set in the center in my application..

From API 16, cursor position set to start of the Hint text...!!!

Set cursor position in edittext android

I'm sure you have resolved the problem after 3 months but I want to answer for someone who faces the same problem.
The problem is with this attribute: android:gravity="top"
When you use it, it doesn't let the cursor to move. I don't know why but after I deleted this line, it worked.



Related Topics



Leave a reply



Submit