I want to add masking .. like 00000-0000000-0
etusercnic.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String str = s.toString();
if (s.length() == 5 || s.length() == 13) {
str += "-";
etusercnic.setText(str);
etusercnic.setSelection(str.length());
}
} catch (Exception ignored) {
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
It works perfectly,when first time i entered the value, but when i remove any digit it placed - sign. so what can i do..
I'm sure you're masking it for CNIC :D
Anyways here is my version of masking, it works perfectly:
etusercnic.setRawInputType(InputType.TYPE_CLASS_NUMBER);
etusercnic.addTextChangedListener(new TextWatcher() {
int len = 0;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String val = etusercnic.getText().toString();
if((val.length()==5 && len <val.length()) || (val.length()==13 && len<val.length())){
etusercnic.append("-");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
String str = etusercnic.getText().toString();
len = str.length();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
The xml of your Edittext should be something similar to this:
<EditText
android:id="@+id/nic_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/signup_cnic_hint"
android:singleLine="true"
android:inputType="date"
android:maxLength="15"
android:textColor="#000000"
android:textColorHint="#808080"
android:textSize="15sp"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With