I am looking into the libphonenumber library (https://github.com/googlei18n/libphonenumber), but I am only able to get it to work for "US" and "BR" regions. How do I get it to work for "FR" region? The format I am seeking is 1 41 02 25 00.
I was able to get this format with my own code
@Override
public void afterTextChanged(Editable s) {
// phone digits without formatting
phone = s.toString().replaceAll("[^\\d]", "");
if (phone.length() == 1) {
edtPhoneNumber.setText(s.toString().concat(" "));
// move cursor to original position relative to the end of the string
edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
} else {
if (pairings.length() >= 2) {
pairings = "";
}
pairings = pairings.concat(phone.substring((phone.length()-1)));
if (pairings.length() >= 2) {
if (phone.length() < 9) {
edtPhoneNumber.setText(s.toString().concat(" "));
} else {
edtPhoneNumber.setText(s.toString());
}
} else {
edtPhoneNumber.setText(s.toString());
}
// move cursor to original position relative to the end of the string
edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
}
}
My implementation of the library is as follows. After instantiating with the region code of interest,
AsYouTypeFormatter aytf = PhoneNumberUtil.getInstance().getAsYouTypeFormatter("FR")
I then have the following code inside of afterTextChanged(Editable s)
if(phone.length() > 0){
for(int i = 0; i < phone.length(); i++){
formattedPhoneNumber = aytf.inputDigit(phone.charAt(i));
}
//The formatted output shows properly in this EditText but not when I try to put it back into the original one (phoneNumberText)
edtPhoneNumber.setText(formattedPhoneNumber);
edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
aytf.clear();
}
formattedPhoneNumber = null;
isPhoneFormatting = false;