I'm sure you are masking it for CNIC: D
In any case, this is my disguise version, it works fine:
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 look something like 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" />
source share