edtTxt.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(s.length() != 0 && s.length() == 2){
String str = s.toString();
str.replaceAll("..(?!$)", "$0:");
edtTxt.setText(str);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
I need to display ":" after 2nd digit that is for example 10:25, maximum length is 5 digits it is edittext. If i started typing in the edittext 10 after this ":" should be inserted then 10:25 should be displayed in the edittext. I tried with the above logic not working. can anyone help me. Thanks in advance
First of all you ignore the result of
str.replaceAll(). The method returns aString.The
ifcondition can be simplified tos.length() == 2.And the regex you are using doesn't work.
This will add colon in the EditText after you have entered 2 characters