Search code examples
androidcountry-codes

android countryPicker from github, bring countries to top


I have successfully implemented the AndroidCountryPicker open source project from github. this one: https://github.com/roomorama/AndroidCountryPicker . Now I would like to bring US, HongKong, and Canada to the top of the list. So the user doesn't have to constantly search, and those are going to be the most used. Anyone know how this would be possible.

Thanks


Solution

  • I think you will need to change the library code to achieve that. The code responsible to load and sort the countries is this (at 23 Dec 14).

    You could ignore those three countries and then add them after sorting the array (supposing you want all the others to appear ordered):

    Country US, HK, CA;
    US = HK = CA = null;
    while (keys.hasNext()) {
        String key = (String) keys.next();
        Country country = new Country();
        country.setCode(key);
        country.setName(jsonObject.getString(key));
        switch(key) {
            case "US": US = country; break;
            case "HK": HK = country; break;
            case "CA": CA = country; break;
            default: allCountriesList.add(country);
        }
    }
    
    Collections.sort(allCountriesList, this);
    allCountriesList.add(0, CA);
    allCountriesList.add(0, HK);
    allCountriesList.add(0, US);
    

    If you are using Java 6 or older you will need to replace the switch with if/else's.

    EDIT: In case anyone wants to change the countries data, it is stored in a big JSON string (encoded in base64) in res/values/strings_countries.xml. So that needs to be decoded, changed and then encoded again (something like base64decode.org or base64 command will do the trick)