Automatically add dash to phone number in Android

Instead of 5118710, it should be 511-8710 . I would like to add a dash after the user has entered 3 digits already in the EditText. The maximum length of an EditText is only 7 digits.

After I understood this problem, I was stuck in coding again. When I already entered 3 digits, it adds a dash (this is what I would like to happen), but my problem here is that the next 3 digits also add a dash (like this: 511-871- ) .. Please help me with this. thanks!

  txt_HomeNo.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { boolean flag = true; String eachBlock[] = txt_HomeNo.getText().toString().split("-"); for (int i = 0; i < eachBlock.length; i++) { if (eachBlock[i].length() > 3) { flag = false; } } if (flag) { 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) { if (((txt_HomeNo.getText().length() + 1) % 4) == 0) { if (txt_HomeNo.getText().toString().split("-").length <= 3) { txt_HomeNo.setText(txt_HomeNo.getText() + "-"); txt_HomeNo.setSelection(txt_HomeNo.getText().length()); } } a = txt_HomeNo.getText().toString(); } else { a = txt_HomeNo.getText().toString(); keyDel = 0; } } else { txt_HomeNo.setText(a); } } 
+6
source share
6 answers

Make the following modified addTextChangedListener for txt_HomeNo . The code below checks if the length of the entered text is 3, and if it then adds - to it. Not a very reliable solution, but it works!

 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 } }); 
+8
source

The simplest solution is to use PhoneNumberFormattingTextWatcher , which will format the number according to the locale of the system.

XML:

 <EditText android:id="@+id/phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/enter_phone_number" android:inputType="phone" /> 

Add addTextChangedListener() to your class:

 EditText phoneNumber = (EditText)findViewById(R.id.phone_number); phoneNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 
+26
source

I have a few small changes in the neo108 solution, so it can work with both a soft keyboard and a hard keyboard, in my code, for example, edittext will follow the rule to automatically add "" at positions 5 and 9.

 txtPhone.addTextChangedListener(new TextWatcher() { int keyDel; @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { txtPhone.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { if (keyCode == KeyEvent.KEYCODE_DEL) { keyDel = 1; } return false; } }); String currentString = txtPhone.getText().toString(); int currentLength = txtPhone.getText().length(); if (currentLength == 5 || currentLength == 9) { keyDel = 1; } if (keyDel == 0) { if (currentLength == 4 || currentLength == 8) { txtPhone.setText(txtPhone.getText() + " "); txtPhone.setSelection(txtPhone.getText().length()); } } else { if (currentLength != 5 && currentLength != 9) { keyDel = 0; } else if ((currentLength == 5 || currentLength == 9) && !" ".equals(currentString.substring(currentLength - 1, currentLength))) { txtPhone.setText(currentString.substring(0, currentLength - 1) + " " + currentString.substring(currentLength - 1, currentLength)); txtPhone.setSelection(txtPhone.getText().length()); } } } 
+4
source

I implemented a custom TextWatcher ; it processes 10 and 11 digits of phone numbers (i.e. 1-555-867-5309 and 555-867-5309 ). Allows you to add, delete, insert, bulk delete, while maintaining the correct cursor position.

  public class CustomPhoneTextWatcher implements TextWatcher { private final EditText editText; private String previousString; public CustomPhoneTextWatcher(EditText editText) { this.editText = editText; } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { // if the previous editable ends with a dash and new is shorter than previous // additionally remove preceding character if (previousString != null && previousString.endsWith("-") && editable.toString().length() < previousString.length()) { previousString = editable.toString(); String removedCharacterPriorToDash = editable.toString().substring(0, editable.length() - 1); editText.setText(removedCharacterPriorToDash); int position = editText.length(); Editable etext = editText.getText(); Selection.setSelection(etext, position); } else { previousString = editable.toString(); String numericString = StringUtils.removeNonnumeric(editable.toString()); int stringLength = numericString.length(); boolean startsWithOne = numericString.startsWith("1"); numericString = numericString.substring(0, Math.min(stringLength, 10 + (startsWithOne ? 1 : 0))); int lastHyphenIndex = 6 + (startsWithOne ? 1 : 0); int secondToLastHyphenIndex = 3 + (startsWithOne ? 1 : 0); if (stringLength >= lastHyphenIndex) { numericString = numericString.substring(0, lastHyphenIndex) + "-" + numericString.substring(lastHyphenIndex, numericString.length()); } if (stringLength >= secondToLastHyphenIndex) { numericString = numericString.substring(0, secondToLastHyphenIndex) + "-" + numericString.substring(secondToLastHyphenIndex, numericString.length()); } if (numericString.startsWith("1")) { numericString = numericString.substring(0, 1) + "-" + numericString.substring(1, numericString.length()); } if (!numericString.equals(editable.toString())) { editText.setText(numericString); int position = editText.length(); Editable etext = editText.getText(); Selection.setSelection(etext, position); } } } } 

StringUtils.removeNonnumeric(editable.toString()) is a call to this method:

  public static String removeNonnumeric(String text) { return text.replaceAll("[^\\d]", ""); } 
+1
source

Do it yourself using OnEditTextChangedListener and insert a dash counting the number of characters. Counting votes in the EditText Changed Listener

0
source

Thanks for all of the above.

  • The editText.setOnKeyListener () function will never be called if your device has only a soft keyboard.
  • If we strictly follow the rule to add a β€œ-” , then this code will not always show the result of the desire.

    editText.addTextChangedListener (new PhoneNumberFormattingTextWatcher ());

but the above code is the best solution for formatting a phone number.

Besides the above of this solution, I am writing code that works with all types of conditions:

 phoneNumber.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) { } @Override public void afterTextChanged(Editable s) { if (len > phoneNumber.getText().length() ){ len--; return; } len = phoneNumber.getText().length(); if (len == 4 || len== 8) { String number = phoneNumber.getText().toString(); String dash = number.charAt(number.length() - 1) == '-' ? "" : "-"; number = number.substring(0, (len - 1)) + dash + number.substring((len - 1), number.length()); phoneNumber.setText(number); phoneNumber.setSelection(number.length()); } } }); 

this line of code must be added with a "-" at the 3rd and 6th position of the number. if (len == 4 || len == 8)

0
source

Source: https://habr.com/ru/post/946739/