Skip to content

Change Flags

Harsh B. Bhakta edited this page Oct 16, 2021 · 3 revisions

Why additional work?

When png flags are packed with library, it increases library size and eventually it increases your project size (even for project that does not want to use those flags). To avoid this, Country Picker library packs only text based Emoji flags by default. You can change flags by providing a FlagPack. This FlagPack can be a custom one OR one of the library provided FlagPacks.

1. Create FlagProvider using FlagPack

1. If you wish you use library provided flag pack then pick one of the following (well there is only one additional right now)

  • a. FlagPack1
    • Add implementation("com.hbb20:android-country-picker-flagpack1:X.Y.Z") Add
    • image
    • Make Flag Image Provider using, val flagProvider = CPFlagImageProvider(FlagPack1.alpha2ToFlag,FlagPack1.missingFlagPlaceHolder)

2. Use custom flag pack

  • For reference, you will need to create a data object similar to library provided flag packs i.e. FlagPack1 Source
  • Let's call it CustomFlagPack.
  • Create a variable for missing flag placeholder image resource val missingFlagPlaceHolder: Int = R.drawable.flag_transparent. This will be used for flag that your flag pack does not have
  • Create a Map<String,Int> named alpha2ToFlag where key will be alpha2 code of country and corresponding value will be flag image resource like this.
  • Make Flag Image Provider using, val flagProvider = CPFlagImageProvider(CustomFlagPack.alpha2ToFlag, CustomFlagPack.missingFlagPlaceHolder)

2. Apply FlagProvider

  • Now that you have flagProvider ready, you can apply it to CountryPickerView, CountryPickerDialog or CountryList.
  • For CountryPickerView, apply using countryPickerView.changeFlagProvider(flagProvider). This will be automatically applied to the dialog launched on click.
  • For CountryPickerDialog, pass it as cpFlagProvider to context.launchCountryPickerDialog(cpFlagProvider = flagProvider). Read More
  • For CountryList, pass it as cpFlagProvider to recyclerView.loadCountries(cpFlagProvider = flagProvider). Read More