Sunday 28 August 2011

How to Set EditText Cursor position in Android


Below Code is Set cursor to Starting in EditText:

        EditText editText = (EditText)findViewById(R.id.edittext_id);
        editText.setSelection(0);

Below Code is Set cursor to end of the EditText:

        EditText editText = (EditText)findViewById(R.id.edittext_id);
        editText.setSelection(editText.getText().length());

Below Code is Set cursor after some 2th Character position :

   EditText editText = (EditText)findViewById(R.id.edittext_id);
   editText.setSelection(2);


Here we done this way to set cursor position to end of the text after 
updating the text of EditText programmatically here, editText is EditText 
Now cursor will be appear after"Updated New Text" String

EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setText("Updated New Text");
int position = editText.getText().length();
Editable editObj= editText.getText();
Selection.setSelection(editObj, position);


2 comments:

  1. Thank you! Your method worked where many failed. You saved me a lot of head scratching and I am grateful.

    ReplyDelete

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...