Skip to content
Advertisement

how to use libphonenumber in android studio to get country code

Basically I don’t know how to use libphonenumber in android studio to get country code like(+91) I have tried and but it’s giving me the first two letter of my input number.

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

Advertisement

Answer

this might be helpful for u

    public static String getMobileNoWithCode(String mobileNo) {
    try {
        List<Country> countries=Countries.getCountries();
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        for (Country country :countries){
        Phonenumber.PhoneNumber phNumberProto = phoneUtil.parse(
                mobileNo, country.getDCode());
        if (phoneUtil.isValidNumber(phNumberProto))
            return country.getDCode()+mobileNo;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

Countries.class

public class Countries {

private static final Country[] COUNTRIES = {
        new Country("AF", "Afghanistan", "+93", "AFN"),
        new Country("AX", "Aland Islands", "+358", "EUR"),
        new Country("AL", "Albania", "+355", "ALL"),
        new Country("DZ", "Algeria", "+213", "DZD"),
        new Country("AS", "American Samoa", "+1", "USD"),
        new Country("AD", "Andorra", "+376", "EUR"),
        new Country("AO", "Angola", "+244", "AOA"),
        new Country("AI", "Anguilla", "+1", "XCD"),
        new Country("AQ", "Antarctica", "+672", "USD"),
        new Country("AG", "Antigua and Barbuda", "+1", "XCD"),
        new Country("AR", "Argentina", "+54", "ARS"),
        new Country("AM", "Armenia", "+374", "AMD"),
        new Country("AW", "Aruba", "+297", "AWG"),
        new Country("AU", "Australia", "+61", "AUD"),
        new Country("AT", "Austria", "+43", "EUR"),
        new Country("AZ", "Azerbaijan", "+994", "AZN"),
        new Country("BS", "Bahamas", "+1", "BSD"),
        new Country("BH", "Bahrain", "+973", "BHD"),
        new Country("BD", "Bangladesh", "+880", "BDT"),
        new Country("BB", "Barbados", "+1", "BBD"),
        new Country("BY", "Belarus", "+375", "BYR"),
        new Country("BE", "Belgium", "+32", "EUR"),
        new Country("BZ", "Belize", "+501", "BZD"),
        new Country("BJ", "Benin", "+229", "XOF"),
        new Country("BM", "Bermuda", "+1", "BMD"),
        new Country("BT", "Bhutan", "+975", "BTN"),
        new Country("BO", "Bolivia, Plurinational State of", "+591", "BOB"),
        new Country("BA", "Bosnia and Herzegovina", "+387", "BAM"),
        new Country("BQ", "Bonaire", "+599", "USD"),
        new Country("BW", "Botswana", "+267", "BWP"),
        new Country("BV", "Bouvet Island", "+47", "NOK"),
        new Country("BR", "Brazil", "+55", "BRL"),
        new Country("IO", "British Indian Ocean Territory", "+246", "USD"),
        new Country("BN", "Brunei Darussalam", "+673", "BND"),
        new Country("BG", "Bulgaria", "+359", "BGN"),
        new Country("BF", "Burkina Faso", "+226", "XOF"),
        new Country("BI", "Burundi", "+257", "BIF"),
        new Country("KH", "Cambodia", "+855", "KHR"),
        new Country("CM", "Cameroon", "+237", "XAF"),
        new Country("CA", "Canada", "+1", "CAD"),
        new Country("CV", "Cape Verde", "+238", "CVE"),
        new Country("KY", "Cayman Islands", "+345", "KYD"),
        new Country("CF", "Central African Republic", "+236", "XAF"),
        new Country("TD", "Chad", "+235", "XAF"),
        new Country("CL", "Chile", "+56", "CLP"),
        new Country("CN", "China", "+86", "CNY"),
        new Country("CX", "Christmas Island", "+61", "AUD"),
        new Country("CC", "Cocos (Keeling) Islands", "+61", "AUD"),
        new Country("CO", "Colombia", "+57", "COP"),
        new Country("KM", "Comoros", "+269", "KMF"),
        new Country("CD", "Congo, The Democratic Republic of the", "+243", "CDF"),
        new Country("CG", "Congo", "+242", "XAF"),
        new Country("CK", "Cook Islands", "+682", "NZD"),
        new Country("CR", "Costa Rica", "+506", "CRC"),
        new Country("HR", "Croatia", "+385", "HRK"),
        new Country("CU", "Cuba", "+53", "CUP"),
        new Country("CW", "Curacao", "+599", "ANG"),
        new Country("CY", "Cyprus", "+357", "EUR"),
        new Country("CZ", "Czech Republic", "+420", "CZK"),
        new Country("DK", "Denmark", "+45", "DKK"),
        new Country("DJ", "Djibouti", "+253", "DJF"),
        new Country("DM", "Dominica", "+1", "XCD"),
        new Country("DO", "Dominican Republic", "+1", "DOP"),
        new Country("TL", "East Timor", "+670", "USD"),
        new Country("EC", "Ecuador", "+593", "USD"),
        new Country("EG", "Egypt", "+20", "EGP"),
        new Country("SV", "El Salvador", "+503", "SVC"),
        new Country("GQ", "Equatorial Guinea", "+240", "XAF"),
        new Country("ER", "Eritrea", "+291", "ERN"),
        new Country("EE", "Estonia", "+372", "EUR"),
        new Country("ET", "Ethiopia", "+251", "ETB"),
        new Country("FK", "Falkland Islands (Malvinas)", "+500", "FKP"),
        new Country("FO", "Faroe Islands", "+298", "DKK"),
        new Country("FJ", "Fiji", "+679", "FJD"),
        new Country("FI", "Finland", "+358", "EUR"),
        new Country("FR", "France", "+33", "EUR"),
        new Country("GF", "French Guiana", "+594", "EUR"),
        new Country("TF", "French Southern Territories", "+262", "EUR"),
        new Country("PF", "French Polynesia", "+689", "XPF"),
        new Country("GA", "Gabon", "+241", "XAF"),
        new Country("GM", "Gambia", "+220", "GMD"),
        new Country("GE", "Georgia", "+995", "GEL"),
        new Country("DE", "Germany", "+49", "EUR"),
        new Country("GH", "Ghana", "+233", "GHS"),
        new Country("GI", "Gibraltar", "+350", "GIP"),
        new Country("GR", "Greece", "+30", "EUR"),
        new Country("GL", "Greenland", "+299", "DKK"),
        new Country("GD", "Grenada", "+1", "XCD"),
        new Country("GP", "Guadeloupe", "+590", "EUR"),
        new Country("GU", "Guam", "+1", "USD"),
        new Country("GT", "Guatemala", "+502", "GTQ"),
        new Country("GG", "Guernsey", "+44", "GGP"),
        new Country("GN", "Guinea", "+224", "GNF"),
        new Country("GW", "Guinea-Bissau", "+245", "XOF"),
        new Country("GY", "Guyana", "+595", "GYD"),
        new Country("HT", "Haiti", "+509", "HTG"),
        new Country("HM", "Heard Island and McDonald Islands", "+000", "AUD"),
        new Country("VA", "Holy See (Vatican City State)", "+379", "EUR"),
        new Country("HN", "Honduras", "+504", "HNL"),
        new Country("HK", "Hong Kong", "+852", "HKD"),
        new Country("HU", "Hungary", "+36", "HUF"),
        new Country("IS", "Iceland", "+354", "ISK"),
        Utils.getDefaultCountry(),
        new Country("ID", "Indonesia", "+62", "IDR"),
        new Country("IR", "Iran, Islamic Republic of", "+98", "IRR"),
        new Country("IQ", "Iraq", "+964", "IQD"),
        new Country("IE", "Ireland", "+353", "EUR"),
        new Country("IM", "Isle of Man", "+44", "GBP"),
        new Country("IL", "Israel", "+972", "ILS"),
        new Country("IT", "Italy", "+39", "EUR"),
        new Country("CI", "Ivory Coast", "+225", "XOF"),
        new Country("JM", "Jamaica", "+1", "JMD"),
        new Country("JP", "Japan", "+81", "JPY"),
        new Country("JE", "Jersey", "+44", "JEP"),
        new Country("JO", "Jordan", "+962", "JOD"),
        new Country("KZ", "Kazakhstan", "+7", "KZT"),
        new Country("KE", "Kenya", "+254", "KES"),
        new Country("KI", "Kiribati", "+686", "AUD"),
        new Country("XK", "Kosovo", "+383", "EUR"),
        new Country("KW", "Kuwait", "+965", "KWD"),
        new Country("KG", "Kyrgyzstan", "+996", "KGS"),
        new Country("LA", "Lao People's Democratic Republic", "+856", "LAK"),
        new Country("LV", "Latvia", "+371", "LVL"),
        new Country("LB", "Lebanon", "+961", "LBP"),
        new Country("LS", "Lesotho", "+266", "LSL"),
        new Country("LR", "Liberia", "+231", "LRD"),
        new Country("LY", "Libyan Arab Jamahiriya", "+218", "LYD"),
        new Country("LI", "Liechtenstein", "+423", "CHF"),
        new Country("LT", "Lithuania", "+370", "LTL"),
        new Country("LU", "Luxembourg", "+352", "EUR"),
        new Country("MO", "Macao", "+853", "MOP"),
        new Country("MK", "Macedonia, The Former Yugoslav Republic of", "+389",
                "MKD"),
        new Country("MG", "Madagascar", "+261", "MGA"),
        new Country("MW", "Malawi", "+265", "MWK"),
        new Country("MY", "Malaysia", "+60", "MYR"),
        new Country("MV", "Maldives", "+960", "MVR"),
        new Country("ML", "Mali", "+223", "XOF"),
        new Country("MT", "Malta", "+356", "EUR"),
        new Country("MH", "Marshall Islands", "+692", "USD"),
        new Country("MQ", "Martinique", "+596", "EUR"),
        new Country("MR", "Mauritania", "+222", "MRO"),
        new Country("MU", "Mauritius", "+230", "MUR"),
        new Country("YT", "Mayotte", "+262", "EUR"),
        new Country("MX", "Mexico", "+52", "MXN"),
        new Country("FM", "Micronesia, Federated States of", "+691", "USD"),
        new Country("MD", "Moldova, Republic of", "+373", "MDL"),
        new Country("MC", "Monaco", "+377", "EUR"),
        new Country("MN", "Mongolia", "+976", "MNT"),
        new Country("ME", "Montenegro", "+382", "EUR"),
        new Country("MS", "Montserrat", "+1", "XCD"),
        new Country("MA", "Morocco", "+212", "MAD"),
        new Country("MZ", "Mozambique", "+258", "MZN"),
        new Country("MM", "Myanmar", "+95", "MMK"),
        new Country("NA", "Namibia", "+264", "NAD"),
        new Country("NR", "Nauru", "+674", "AUD"),
        new Country("NP", "Nepal", "+977", "NPR"),
        new Country("NL", "Netherlands", "+31", "EUR"),
        new Country("NC", "New Caledonia", "+687", "XPF"),
        new Country("NZ", "New Zealand", "+64", "NZD"),
        new Country("NI", "Nicaragua", "+505", "NIO"),
        new Country("NE", "Niger", "+227", "XOF"),
        new Country("NG", "Nigeria", "+234", "NGN"),
        new Country("NU", "Niue", "+683", "NZD"),
        new Country("NF", "Norfolk Island", "+672", "AUD"),
        new Country("MP", "Northern Mariana Islands", "+1", "USD"),
        new Country("KP", "North Korea", "+850", "KPW"),
        new Country("NO", "Norway", "+47", "NOK"),
        new Country("OM", "Oman", "+968", "OMR"),
        new Country("PK", "Pakistan", "+92", "PKR"),
        new Country("PW", "Palau", "+680", "USD"),
        new Country("PS", "Palestinian Territory, Occupied", "+970", "ILS"),
        new Country("PA", "Panama", "+507", "PAB"),
        new Country("PG", "Papua New Guinea", "+675", "PGK"),
        new Country("PY", "Paraguay", "+595", "PYG"),
        new Country("PE", "Peru", "+51", "PEN"),
        new Country("PH", "Philippines", "+63", "PHP"),
        new Country("PN", "Pitcairn", "+872", "NZD"),
        new Country("PL", "Poland", "+48", "PLN"),
        new Country("PT", "Portugal", "+351", "EUR"),
        new Country("PR", "Puerto Rico", "+1", "USD"),
        new Country("QA", "Qatar", "+974", "QAR"),
        new Country("RO", "Romania", "+40", "RON"),
        new Country("RU", "Russia", "+7", "RUB"),
        new Country("RW", "Rwanda", "+250", "RWF"),
        new Country("RE", "Reunion", "+262", "EUR"),
        new Country("BL", "Saint Barthelemy", "+590", "EUR"),
        new Country("SH", "Saint Helena, Ascension and Tristan Da Cunha", "+290",
                "SHP"),
        new Country("KN", "Saint Kitts and Nevis", "+1", "XCD"),
        new Country("LC", "Saint Lucia", "+1", "XCD"),
        new Country("MF", "Saint Martin", "+590", "EUR"),
        new Country("PM", "Saint Pierre and Miquelon", "+508", "EUR"),
        new Country("VC", "Saint Vincent and the Grenadines", "+1", "XCD"),
        new Country("WS", "Samoa", "+685", "WST"),
        new Country("SM", "San Marino", "+378", "EUR"),
        new Country("ST", "Sao Tome and Principe", "+239", "STD"),
        new Country("SA", "Saudi Arabia", "+966", "SAR"),
        new Country("SN", "Senegal", "+221", "XOF"),
        new Country("RS", "Serbia", "+381", "RSD"),
        new Country("SC", "Seychelles", "+248", "SCR"),
        new Country("SL", "Sierra Leone", "+232", "SLL"),
        new Country("SG", "Singapore", "+65", "SGD"),
        new Country("SX", "Sint Maarten", "+1", "ANG"),
        new Country("SK", "Slovakia", "+421", "EUR"),
        new Country("SI", "Slovenia", "+386", "EUR"),
        new Country("SB", "Solomon Islands", "+677", "SBD"),
        new Country("SO", "Somalia", "+252", "SOS"),
        new Country("ZA", "South Africa", "+27", "ZAR"),
        new Country("SS", "South Sudan", "+211", "SSP"),
        new Country("GS", "South Georgia and the South Sandwich Islands", "+500",
                "GBP"),
        new Country("KR", "South Korea", "+82", "KRW"),
        new Country("ES", "Spain", "+34", "EUR"),
        new Country("LK", "Sri Lanka", "+94", "LKR"),
        new Country("SD", "Sudan", "+249", "SDG"),
        new Country("SR", "Suriname", "+597", "SRD"),
        new Country("SJ", "Svalbard and Jan Mayen", "+47", "NOK"),
        new Country("SZ", "Swaziland", "+268", "SZL"),
        new Country("SE", "Sweden", "+46", "SEK"),
        new Country("CH", "Switzerland", "+41", "CHF"),
        new Country("SY", "Syrian Arab Republic", "+963", "SYP"),
        new Country("TW", "Taiwan", "+886", "TWD"),
        new Country("TJ", "Tajikistan", "+992", "TJS"),
        new Country("TZ", "Tanzania, United Republic of", "+255", "TZS"),
        new Country("TH", "Thailand", "+66", "THB"),
        new Country("TG", "Togo", "+228", "XOF"),
        new Country("TK", "Tokelau", "+690", "NZD"),
        new Country("TO", "Tonga", "+676", "TOP"),
        new Country("TT", "Trinidad and Tobago", "+1", "TTD"),
        new Country("TN", "Tunisia", "+216", "TND"),
        new Country("TR", "Turkey", "+90", "TRY"),
        new Country("TM", "Turkmenistan", "+993", "TMT"),
        new Country("TC", "Turks and Caicos Islands", "+1", "USD"),
        new Country("TV", "Tuvalu", "+688", "AUD"),
        new Country("UM", "U.S. Minor Outlying Islands", "+1", "USD"),
        new Country("UG", "Uganda", "+256", "UGX"),
        new Country("UA", "Ukraine", "+380", "UAH"),
        new Country("AE", "United Arab Emirates", "+971", "AED"),
        new Country("GB", "United Kingdom", "+44", "GBP"),
        new Country("US", "United States", "+1", "USD"),
        new Country("UY", "Uruguay", "+598", "UYU"),
        new Country("UZ", "Uzbekistan", "+998", "UZS"),
        new Country("VU", "Vanuatu", "+678", "VUV"),
        new Country("VE", "Venezuela, Bolivarian Republic of", "+58", "VEF"),
        new Country("VN", "Vietnam", "+84", "VND"),
        new Country("VG", "Virgin Islands, British", "+1", "USD"),
        new Country("VI", "Virgin Islands, U.S.", "+1", "USD"),
        new Country("WF", "Wallis and Futuna", "+681", "XPF"),
        new Country("EH", "Western Sahara", "+212", "MAD"),
        new Country("YE", "Yemen", "+967", "YER"),
        new Country("ZM", "Zambia", "+260", "ZMW"),
        new Country("ZW", "Zimbabwe", "+263", "USD"),

};

public static List<Country> getCountries() {
    return new ArrayList<>(Arrays.asList(COUNTRIES));
}}

Country.class

public class Country implements Parcelable {

public static final Creator<Country> CREATOR = new Creator<Country>() {
    @Override
    public Country createFromParcel(Parcel in) {
        return new Country(in);
    }

    @Override
    public Country[] newArray(int size) {
        return new Country[size];
    }
};
private String name;
private String code;
@SerializedName("dial_code")
private String dCode;

public Country(String code, String name, String dCode, String eur) {
    this.code = code;
    this.name = name;
    this.dCode = dCode;
}

private Country(Parcel in) {
    name = in.readString();
    code = in.readString();
    dCode = in.readString();
}

public String getName() {
    return name;
}

public String getCode() {
    return code;
}

public String getDCode() {
    return dCode;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(code);
    dest.writeString(dCode);
}}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement