2

When i request for a focus for an editext few times it is moving the cursor to beginning even if there is some text. How can we place the cursor in such a way that it will be immediately after the text that is already present in it??

Sai Mukesh
  • 361
  • 3
  • 11
  • 2
    Possible duplicate of http://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext – Orkun Ozen Feb 06 '12 at 16:01

4 Answers4

9

This will set the cursor to the last position of the text:

editText.setSelection(editText.getText().length());
Avi Parshan
  • 1,427
  • 2
  • 19
  • 23
Padma Kumar
  • 20,420
  • 16
  • 69
  • 127
0

Edit Text has a property called append.I think the following line of code will also serve the purpose.

editText.append("");
Sreekanth Karumanaghat
  • 3,217
  • 5
  • 35
  • 68
0

In Layout EditText give android:ellipsize property as end like this android:ellipsize="end"

Hope this will help

Lukas Knuth
  • 24,328
  • 14
  • 80
  • 107
Arvind
  • 1
0

I had a similar requirement, when user wants to edit and clicks on EditText, cursor should be at the end. I achieved the same using below:

edtDisplayName.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN) {

                edtDisplayName.setSelection(edtDisplayName.getText()
                        .length());

                edtDisplayName.requestFocus();

                //Open key board for user to edit
                Commons.showKeyBoard(OnBoardActivity.this, edtDisplayName);

                return true;

            }

            return false;
        }
    });

Here's how I opened the key-board:

public static void showKeyBoard(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, 0);
}
Atul O Holic
  • 6,216
  • 4
  • 36
  • 72