5

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
  • 401
  • 4
  • 11
  • 2
    Possible duplicate of http://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext – Orkun Feb 06 '12 at 16:01

4 Answers4

12

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

editText.setSelection(editText.getText().length());
A P
  • 2,131
  • 2
  • 24
  • 36
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
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,383
  • 6
  • 44
  • 72
0

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

Hope this will help

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
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,692
  • 4
  • 39
  • 74