I have a number that I need to format as a telephone number. If I do
PhoneNumberUtils.formatNumber(numStr);
Then I get
888-555-1234
But what I need to get is
(888) 555-1234
How do I get the second one? Is there a standard android way?
You simply use this and get you want :
or Have look at this url :
http://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html
Try to use regex. This will help you. As for me, i use this:
Don't know if you found what you were looking for, but I ended up writing a little method that takes the length of a string (since the phone numbers I get come from a web service and can be a variety of formats). I believe it should work (so far all my test cases have been with the first two options -- haven't tested the other two yet).
If you have the String
"888-555-1234"
- by usingPhoneNumberUtils.formatNumber(numStr);
you can simply do this:However, this is hard coded. You would need to make sure the String had a full 10 digits before doing so.
If you know the country for which you want to do it, you can use Google's open source library libphonenumber . Here is how you can format it:
If you don't know the country then for
numberStr
use E.164 format phone number and in place of country code use null.