How to show number in specific format only? #256
-
A RawContact has They all are the same number. I want to show the number to the user in my Contacts app with "0340 4140523" this format only. How do I achieve that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @mianaliasjad! Thanks for being a user of this library 😁 I converted the issue you raised, #255, to this Q&A discussion because I believe that number formatting is more the responsibility of the application than this library.
To answer this question effectively, we should look at how phone numbers are displayed to the user and stored in the Contacts provider database in the AOSP Contacts app, Google Contacts app, and Samsung Contacts app. What typical contacts applications do with phone numbersIn the following recordings, I took a look at how these Contacts apps behave when the user enters the same phone number in the same format and in different formats.
As you can see, all contacts applications allow users to put the same number in different formats.I did not record this part but they will also allow you to enter the same exact number in the same exact format. Actually, if you watch closely at the end of the Samsung Contacts recording the last two phone numbers that were saved in a different format (space vs no space) are saved in the same format (no space). I also checked the Contacts Provider data table and the duplicate phone numbers do exist. What we can conclude from this investigation is that it is up to the contacts applications (including users of this library) to perform formatting and duplicate checks if they (you) want. What (you) can do with phone numbersIt is 100% up to you how you want to display phone numbers. This library will simply provide you easy read/write access to the Contacts Provider database. I recommend you take a look at Here are some of the things you can do with it; import android.telephony.PhoneNumberUtils
// "PAK" is the ISO-3166 code for Pakistan numbers (+92).
val countryIso = "PAK"
// In production, you should use the following to determine the ISO at runtime
// val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
// val countryIso = telephonyManager.networkCountryIso
Log.d("FORMATTED_NUMBER", PhoneNumberUtils.formatNumberToE164("+923404140523", countryIso).toString())
Log.d("FORMATTED_NUMBER", PhoneNumberUtils.formatNumber("3404140523").toString())
Log.d("FORMATTED_NUMBER", PhoneNumberUtils.formatNumber("340 4140523").toString())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Log.d("ARE_SAME_NUMBER", PhoneNumberUtils.areSamePhoneNumber("+923404140523", "03404140523", countryIso).toString())
Log.d("ARE_SAME_NUMBER", PhoneNumberUtils.areSamePhoneNumber("0340 4140523", "03404140523", countryIso).toString())
} else {
Log.d("ARE_SAME_NUMBER", PhoneNumberUtils.compare("+923404140523", "03404140523").toString())
Log.d("ARE_SAME_NUMBER", PhoneNumberUtils.compare("0340 4140523", "03404140523").toString())
} The above logs will print...
There are a lot of functions in Good luck! I hope this helps! If it answers your question, please mark this as the answer 😁 |
Beta Was this translation helpful? Give feedback.
Hello @mianaliasjad! Thanks for being a user of this library 😁 I converted the issue you raised, #255, to this Q&A discussion because I believe that number formatting is more the responsibility of the application than this library.
To answer this question effectively, we should look at how phone numbers are displayed to the user and stored in the Contacts provider database in the AOSP Contacts app, Google Contacts app, and Samsung Contacts app.
What typical contacts applications do with phone numbers
In the following recordings, I took a look at how these Contacts apps b…