Search code examples
androidandroid-edittexttext-cursor

EditTextPreference and default cursor position


This is mostly a pet peeve of mine, but it annoys me to no end that the default behavior or EditTextPreferences is to put the cursor at the beginning of the string. This makes NO sense at all to me. In almost any other interface known to man (fine, ME), focusing on a textfield automatically sends the cursor to the END.

So. Is there an (easy) way to override this? I know I can extend from EditTextPreference and call setSelection() manually, but this seems like a really complicated solution for such a simple problem.


Solution

  • I ended up extending EditTextPreference and overriding the showDialog method:

      @Override
      protected void showDialog(Bundle state) {
        super.showDialog(state);
        Handler delayedRun = new Handler();
        delayedRun.post(new Runnable() {
          @Override
          public void run() {
            EditText textBox = getEditText();
            textBox.setSelection(textBox.getText().length());
          }
        });
      }