Reputation: 847
In my app, the user enters his phone number. For example, If he enters: 123456 I want him to see: 12 34 56 How can I do that?
Upvotes: 2
Views: 4719
Reputation: 13485
you can look at libphonenumber library from google code. https://github.com/googlei18n/libphonenumber.
I did a small app using that for android and it works pretty well.
The XX XX XX XX number format is usually used in France. so you can do something like
String formattedNumber = phoneUtil.parse(numberProto, "FR");
They also have a 'format as you type' feature, which may be what you are interested in too. do check out their examples.
//Formatting Phone Numbers 'as you type'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
System.out.println(formatter.inputDigit('6')); // Outputs "6"
... // Input more digits
System.out.println(formatter.inputDigit('3')); // Now outputs "650 253"
edit
you can use the link below https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html to try out what the code returns. (of interest is the output of the "asYouTypeFormatter"
Upvotes: 0
Reputation: 9257
Try this its working based on locale :
etNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
You can also use with country code. https://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html
Upvotes: 2
Reputation: 116
You should have a look on the PhoneNumberUtils
in the android.telephony
API: https://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
Edit:
There you find the method formatNumber
which you can use to display the phone number.
This method tries to format the number according to the rules for the country it is from. If this isn't possible it return the raw number.
public static String formatNumber (String phoneNumber)
This method tries to format the number according the given country's default convention (the country parameter is to set with a two letter country code like US
, GB
or DE
. For further information look here: https://en.wikipedia.org/wiki/ISO_3166-1). If the number is not valid the method return null
.
public static String formatNumber (String phoneNumber, String defaultCountryIso)
Upvotes: 1
Reputation: 3313
You can add text watcher to your edit text and check the size of text till 2 place and add space in it like as follows
callphoneEditText.addTextChangedListener(new CustomTextWatcherForPhoneNumber());
The following class does the required thing you needed
public static class CustomTextWatcherForPhoneNumber implements TextWatcher {
// Change this to what you want... ' ', '-' etc..
private static final char space = ' ';
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// Remove spacing char
if (s.length() > 0 && (s.length() % 5) == 0) {
final char c = s.charAt(s.length() - 1);
if (space == c) {
s.delete(s.length() - 1, s.length());
}
}
// Insert char where needed.
if (s.length() > 0 && (s.length() % 5) == 0) {
char c = s.charAt(s.length() - 1);
// Only if its a digit where there should be a space we insert a
// space
if (Character.isDigit(c)
&& TextUtils.split(s.toString(), String.valueOf(space)).length <= 2) { ****// At this point you will check values and add space between them****
s.insert(s.length() - 1, String.valueOf(space));
}
}
}
}
Check it and let me know if it is helpful to you or not
Upvotes: 1