Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Place cursor at the end of text in EditText

#1
I am changing the value of an `EditText` on `keyListener`.

But when I change the text the cursor is moving to the beginning of the `EditText`.
I need the cursor to be at the end of the text.

How to move the cursor to the end of the text in a `EditText`.
Reply

#2
There is a function called append for ediitext which appends the string value to current edittext value and places the cursor at the end of the value.
You can have the string value as the current ediitext value itself and call append();

myedittext.append("current_this_edittext_string");

Reply

#3
i think this can achieve what you want.


Editable etext = mSubjectTextEditor.getText();
Selection.setSelection(etext, etext.length());

Reply

#4
You could also place the cursor at the end of the text in the `EditText` view like this:

EditText et = (EditText)findViewById(R.id.textview);
int textLength = et.getText().length();
et.setSelection(textLength, textLength);
Reply

#5
/**
* Set cursor to end of text in edittext when user clicks Next on Keyboard.
*/
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
((EditText) view).setSelection(((EditText) view).getText().length());
}
}
};

mEditFirstName.setOnFocusChangeListener(onFocusChangeListener);
mEditLastName.setOnFocusChangeListener(onFocusChangeListener);

It work good for me!
Reply

#6
similar to @Anh Duy's answer, but it didnt work for me. i also needed the cursor to move to the end only when the user taps the edit text and still be able to select the position of the cursor afterwards, this is the only code that has worked for me

boolean textFocus = false; //define somewhere globally in the class

//in onFinishInflate() or somewhere
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

editText.onTouchEvent(event);

if(!textFocus) {
editText.setSelection(editText.getText().length());
textFocus = true;
}

return true;
}
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
textFocus = false;
}
});
Reply

#7
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
editText.setSelection(editText.getText().length());
return false;
}
});
Reply

#8
This does the trick **safely**:

editText.setText("");
if (!TextUtils.isEmpty(text)) {
editText.append(text);
}
Reply

#9
If you want to select all text and just entering new text instead of old one, you can use

android:selectAllOnFocus="true"
Reply

#10
This is another possible solution:

et.append("");

Just try this solution if it doesn't work for any reason:

et.setSelection(et.getText().length());
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through