Coder Social home page Coder Social logo

jboss42 / country-picker-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mukeshsolanki/country-picker-android

0.0 2.0 0.0 554 KB

A simple library that displays a beautiful list of all the countries allowing the user to pick the country he wishes and provide details like country code, iso code name and flag.

License: Apache License 2.0

Java 100.00%

country-picker-android's Introduction

Country Picker for Android



CountryPicker is a simple library that can be show a country picker. See the example to see more detail.

How to use

Integrating the project is simple a refined all you need to do is follow the below steps

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url "https://jitpack.io" }
  }
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.mukeshsolanki:country-picker-android:1.1.7'
}

Once the project has been added to gradle the user can implement this with easy.

CountryPicker picker = CountryPicker.newInstance("Select Country");
picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
picker.setListener(new CountryPickerListener() {
    @Override
    public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
        // Implement your code here
    }
});

That's it your all done.

Operations

The following code will get the current users country details based on sim.

CountryPicker picker = CountryPicker.newInstance("Select Country");
List<Country> countries = picker.getAllCountries(); //Get all countries
Country country = picker.getUserCountryInfo(this); //Get user country based on sim
Country country = picker.getCountryByLocale(context , local); //Get country based on Locale
Country country = picker.getCountryByName(context , country_name); //Get country by country name

//TODO use the country object

Get country from english name

  public Country getCountryByName ( Context context, String countryName ) {
    this.context = context;
    Map<String, String> countries = new HashMap<>();
    for (String iso : Locale.getISOCountries()) {
      Locale l = new Locale("", iso);
      countries.put(l.getDisplayCountry(), iso);
    }

    String countryIsoCode = countries.get(countryName);
    if (countryIsoCode != null) {
      return getCountry(countryIsoCode);
    }
    return afghanistan();
  }

After you have initially selected a country, you may want to store it and retrieve the flag or other details later. This method allows you to store the selected country by its name, and later retrieve it with that same name. Try it as follows

CountryPicker picker = CountryPicker.newInstance("Select Country");
Country country = picker.getCountryByName("Canada");
//TODO use the country object

Get country from locale

  public Country getCountryByLocale( Context context, Locale locale ) {
    this.context = context;
    String countryIsoCode = locale.getISO3Country().substring(0,2).toLowerCase();
    return getCountry(countryIsoCode);
  }

The Android Locale class uses ISO country name standards. This is useful for storing and then retriving the country that was first selected using the country picker. Pass your desired locale into this method to retrieve its country class and contained flag. Try it as follows

CountryPicker picker = CountryPicker.newInstance("Select Country");
Country country = picker.getCountryByLocale(Locale.GERMANY);
//TODO use the country object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.