I am using editText
with addTextChangedListener()
how to format and achieve the pattern what I am looking for.
It should work while adding and deleting also example on character length is 2 hyphen should be added and when I delete and add again its should be added again.
Asked
Active
Viewed 638 times
-1
-
what format number you want to do. see this solution https://stackoverflow.com/questions/6106859/how-to-format-a-phone-number-using-phonenumberutils – Vishal Thakkar Jan 31 '19 at 13:01
3 Answers
0
Please check it.
txt_HomeNo.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
txt_HomeNo.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDel = 1;
return false;
}
});
if (keyDel == 0) {
int len = txt_HomeNo.getText().length();
if(len == 3) {
txt_HomeNo.setText(txt_HomeNo.getText() + "-");
txt_HomeNo.setSelection(txt_HomeNo.getText().length());
}
} else {
keyDel = 0;
}
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});

Parama Sudha
- 2,583
- 3
- 29
- 48
-
tried above solution there is some problem with code keyDel should be int variable? adding its working fine but deleting and adding its again not giving hpyepn on excepted length of character – hema Jan 31 '19 at 13:02
-
same code used as above pasted along with this line private int keyDel = 0; – hema Jan 31 '19 at 13:09
-
tried above solution there is some problem with code keyDel should be int variable? adding its working fine but deleting and adding its again not giving hpyepn on excepted length of character – hema Jan 31 '19 at 13:12
-
example I have taken hypen at 2,7 when I add number as 95-35 when I delete 3 and 5 and when I m again inputing number hypen is not appending and its getting happened at 7 element as 9535536- – hema Jan 31 '19 at 13:16
0
You can add your own custom filters to EditText
This example of the filter adds "-" after you type 2 characters
InputFilter [] phoneFilter = new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start ){
String resultingTxt = dest.toString().substring(0, dstart) + source.subSequence(start, end) + dest.toString().substring(dend);
if (resultingTxt.length() == 2)
return source.subSequence(start, end) + "-";
}
return null;
}
}};
editText.setFilters(phoneFilter);

Vygintas B
- 1,624
- 13
- 31
0
I think you can use some predefined TextChange watcher PhoneNumberFormattingTextWatcher
EditText inputField = (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
as reference this answer comes from https://stackoverflow.com/a/4674114/6656818