Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove country code , When Pick Phone Number from contacts

enter image description here

I have doubt in that section. How to Remove country code, when I pick phone number from contact list?

Ex: +91 999999999 instead of 9999999999 or +020 9696854549 instead of 9696854549 Can any one know the answer about my question. please give solution to this problem

I attached my code and image here.

     private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
    String phoneNo = null ;
    // getData() method will have the Content Uri of the selected contact
    Uri uri = data.getData();
    //Query the content uri
    cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    // column index of the phone number
    int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER
  phoneNo = cursor.getString(phoneIndex);
   String phoneNumber = phoneNo.replaceAll(" ","");
   mobile_et.setText(phoneNumber);
} catch (Exception e) {
    e.printStackTrace();
}
}
like image 471
New Avatar asked Dec 05 '22 13:12

New


1 Answers

My English is poor, but I will try my best to answer your question.

First of all , add this line to your build.gradle

compile 'com.googlecode.libphonenumber:libphonenumber:8.7.0'

And below is a sample write by kotlin

fun deleteCountry(phone: String) : String{
    val phoneInstance = PhoneNumberUtil.getInstance()
    try {
        val phoneNumber = phoneInstance.parse(phone, null)
        return phoneNumber?.nationalNumber?.toString()?:phone
    }catch (_ : Exception) {
    }
    return phone
}

If phone number not start with a '+' followed by the country calling code then you should pass region information, for example:

val phoneNumber = phoneInstance.parse(phone, "CN")

like image 50
ijustyce Avatar answered May 06 '23 21:05

ijustyce