I have an editText where a user inputs a phone number, but right when they click their first number, I want a '+' to appear in the beginning of the text. I have this code but the '+' is constantly there. I only want it to appear when a user inputs a number, how would I fix this?
final EditText editText = findViewById(R.id.register_edit_phone);
final String prefix = "+";
editText.setText(prefix);
Selection.setSelection(editText.getText(), editText.getText().length());
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().startsWith(prefix)) {
editText.setText(prefix);
Selection.setSelection(editText.getText(), editText.getText().length());
}
}
});