'Cursor resides in editText position at zero

While entering the edit text values using the keypad layout that I have created, the cursor resides at the starting position.The cursor position doesn't moving as per the values entered. Is there any way to implement this. Code is as follows:

String zip;
OnClickListener clickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                final int id = v.getId();
                if (id == R.id.button0) {
                    zip = zip + "0";
                    editZip.setText(zip);
                } else if (id == R.id.button1) {
                    zip = zip + "1";
                    editZip.setText(zip);
                } else if (id == R.id.button2) {
                    zip = zip + "2";
                    editZip.setText(zip);
                } else if (id == R.id.button3) {
                    zip = zip + "3";
                    editZip.setText(zip);
                } else if (id == R.id.button4) {
                    zip = zip + "4";
                    //editZip.requestFocus();
                    editZip.setText(zip);
                } else if (id == R.id.button5) {
                    zip = zip + "5";
                    editZip.setText(zip);
                } else if (id == R.id.button6) {
                    zip = zip + "6";
                    editZip.setText(zip);
                } else if (id == R.id.button7) {
                    zip = zip + "7";
                    editZip.setText(zip);
                } else if (id == R.id.button8) {
                    zip = zip + "8";
                    editZip.setText(zip);
                } else if (id == R.id.button9) {
                    zip = zip + "9";
                    editZip.setText(zip);
                } else if (id == R.id.buttonDel) {
                    if (zip.length() > 0) {
                        zip = zip.substring(0, zip.length()-1);
                        editZip.setText(zip); 
                    }   
                }

            }
        };


Solution 1:[1]

Use setSelection(int) to move cursor position.

editZip.setSelection(editZip.getText().length());

At last into OnClickListener code.


Some SO references

  1. How to set cursor position in EditText?
  2. Place cursor at the end of text in EditText

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community