mohammad jafari mohammad jafari - 25 days ago 13
Android Question

Android Edit Text Cursor position doesn't change in first time

I want to change position of Cursor in

EditText
after click on EditText

I want change position Cursor to between ten - twelve after click, With click in first time.

Image of text

Here's my code:

final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(text1 + text2);
editText.setSelection(text1.length());

editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setSelection(editText.getText().length() - text2.length());
}
});

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable editable) {
if (!editable.toString().contains(text1) || !editable.toString().contains(text2)) {
editText.setText(text1 + text2);
editText.setSelection(text1.length());
}
}
});
}

Answer

If I keep it simple, the following snippet is sufficiently enough to achieve what you want (apparently)

final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(text1 + text2);
// Keep it simple for the selection, it will place the cursor at end of text1
editText.setSelection(text1.length());
// editText.setCursorVisible(true); // I don't think it is required

// You don't need to go for OnTouchListener, OnClickListener will do for you
editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        editText.setSelection(editText.getText().length() - text2.length());
    }
});

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (!editable.toString().contains(text1) || !editable.toString().contains(text2)) {
            editText.setText(text1 + text2);
            editText.setSelection(text1.length());
        }
    }
});

UPDATE 1

The above code works fine if it is the first EditText to gain focus, otherwise, if soft keyboard is already open, the cursor at first is shown at tapped place. Here's a work around for this (not the best solution, but it will work for you)

editText.setCursorVisible(false);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    editText.setSelection(editText.getText().length() - text2.length());
                    editText.setCursorVisible(true);
                }
            });
        } else {
            editText.setCursorVisible(false);
        }
    }
});
Comments