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

Viewed 24483

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();
}
}
9 Answers

Try this....

                if(number.length()>10)
                {
                    int startidx=number.length()-10;
                    String getnumber=number.substring(startidx,number.length());
                    etEmerNumber.setText(getnumber);
                }
                else
                {
                    etEmerNumber.setText(number);
                }

I am just simply extending the answer that If you don't want to use any library then you can do it in this way. In order to match any prefix and since some countries could share partially the same prefix (for example 91 and 91-721), you add all possibilities to the regex in descending order and size.

Follow this Code:

public static String PhoneNumberWithoutCountryCode(String phoneNumberWithCountryCode){//+91 7698989898
        Pattern compile = Pattern.compile("\\+(?:998|996|995|994|993|992|977|976|975|974|973|972|971|970|968|967|966|965|964|963|962|961|960|886|880|856|855|853|852|850|692|691|690|689|688|687|686|685|683|682|681|680|679|678|677|676|675|674|673|672|670|599|598|597|595|593|592|591|590|509|508|507|506|505|504|503|502|501|500|423|421|420|389|387|386|385|383|382|381|380|379|378|377|376|375|374|373|372|371|370|359|358|357|356|355|354|353|352|351|350|299|298|297|291|290|269|268|267|266|265|264|263|262|261|260|258|257|256|255|254|253|252|251|250|249|248|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|218|216|213|212|211|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44\\D?1624|44\\D?1534|44\\D?1481|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1\\D?939|1\\D?876|1\\D?869|1\\D?868|1\\D?849|1\\D?829|1\\D?809|1\\D?787|1\\D?784|1\\D?767|1\\D?758|1\\D?721|1\\D?684|1\\D?671|1\\D?670|1\\D?664|1\\D?649|1\\D?473|1\\D?441|1\\D?345|1\\D?340|1\\D?284|1\\D?268|1\\D?264|1\\D?246|1\\D?242|1)\\D?");
        String number = phoneNumberWithCountryCode.replaceAll(compile.pattern(), "");
        //Log.e(tag, "number::_>" +  number);//OutPut::7698989898
        return number;
    }
String Trimmed = s.toString().trim();
if(Trimmed.length() > 10){
char[] number = Trimmed.toCharArray();
int extra;
int dif = Trimmed.length() - 10 ;
for(int i = dif; i < Trimmed.length() ; i++){
    extra = i-dif;
    number[extra] = number[i];
}
for (int i = 10 ; i < Trimmed.length() ; i++){
    number[i]=' ';
}
String finalNumber = String.valueOf(number);
MobileNumberET.setText(finalNumber.trim());
}

//paste this in your text change listener

Use a library https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
// phone must begin with '+'
PhoneNumber numberProto = phoneUtil.parse(phone, "");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}

If you'd like to perform some extra function on a particular Sim on the mobile you can use some of the many methods below. Might not be a perfect solution but, I'll try my best. First get the country code programmatically :

TelephonyManager tm = (TelephonyManager) 
this.getSystemService(Context.TELEPHONY_SERVICE);
//get the country iso from sim e.g US, NG, FR etc
String countryIso = tm.getSimCountryIso().toUpperCase():
//get countrycode from refegion returns the code e.g +123, +1, +23 etc. 
Int countryCode = 
phoneNumberUtil.getInstance().getCountryCodeForRegion(countryIso);

Then you can remove it using your preferred way likeString PhoneNumber = phoneNo.replaceAll("countryCode ", "" );

If you can use a library, this might help you:

Gradle:

repositories {
  mavenCentral()
}

dependencies {
  implementation 'io.michaelrocks:libphonenumber-android:8.12.51'
}

Java:

String getPhoneNummberWithoutCountryCode(String phoneNo)
{
    PhoneNumberUtil phoneInstance = PhoneNumberUtil.createInstance(this.getContext());
    
    Phonenumber.PhoneNumber phoneNumber = phoneInstance.parse(phoneNo, null);
    String nationalSignificantNumber = phoneInstance.getNationalSignificantNumber(phoneNumber);

    return nationalSignificantNumber;
}

nationalSignificantNumber is the required phone number

Related