Using PhoneNumberUtils.formatNumber() on API 16

Brandon picture Brandon · Dec 30, 2015 · Viewed 9.9k times · Source

I'm trying to format numbers to a default country code, and I know how, but when I do it, an error appears saying this is only for API 21. I am targeting API 16. If I use the old method, I get an error saying the method is deprecated? How can I use that method on API 16?

Thanks!

The docs: http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html#FORMAT_NANP

Answer

GFPF picture GFPF · Apr 14, 2016

Following example with deprecated method as mentioned by @qbix.

A good practice is to check the level of the sdk to use the correct method:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    yourTextView.setText(PhoneNumberUtils.formatNumber(yourStringPhone, Locale.getDefault().getCountry()));
} else {
    yourTextView.setText(PhoneNumberUtils.formatNumber(yourStringPhone)); //Deprecated method
}