Skip to content

Instantly share code, notes, and snippets.

@JGeraldoLima
Last active March 20, 2018 16:13
Show Gist options
  • Save JGeraldoLima/7eb4dba516367f9b699c168fd2c12c61 to your computer and use it in GitHub Desktop.
Save JGeraldoLima/7eb4dba516367f9b699c168fd2c12c61 to your computer and use it in GitHub Desktop.
Android Country preference using 100% native resources, with no need to read countries from external files. Automatically sorted and translated.
package <your_package>;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
/* You can use default ListPreference if you prefer.
Got this lib from <link>https://github.com/afollestad/material-dialogs</link>
*/
import com.afollestad.materialdialogs.prefs.MaterialListPreference;
import com.br.batendomacrosandroid.utils.Util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class CountryPreference extends MaterialListPreference {
public CountryPreference(Context context) {
super(context);
this.init();
}
public CountryPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.init();
}
private void init() {
Locale[] locales = Locale.getAvailableLocales();
Map<String, String> unsortedCountriesMap = new HashMap<>();
for (Locale locale : locales) {
String countryCode = locale.getCountry();
String country = locale.getDisplayCountry();
if (country.trim().length() > 0 && unsortedCountriesMap.get(countryCode) == null) {
unsortedCountriesMap.put(country, countryCode);
}
}
Map<String, String> sortedCountriesMap = new TreeMap<>(unsortedCountriesMap);
String[] countriesCodesArray = new String[unsortedCountriesMap.size()];
String[] countriesNamesArray = new String[unsortedCountriesMap.size()];
this.setEntryValues(sortedCountriesMap.values().toArray(countriesCodesArray));
this.setEntries(sortedCountriesMap.keySet().toArray(countriesNamesArray));
}
}
/* To use on your pref.xml */
/***************************************************
<<your_package>.CountryPreference
android:defaultValue=""
android:key="user_country"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:summary=""
android:title="@string/pref_country"/>
/**************************************************/
/* Here's an util method to get a Country name to set on your PreferencesFragment */
/****************************************
private String getCountryNameByCode() {
String currentPreferenceValue = userSharedPreferences.getString(Constants.PREFS_USER_COUNTRY, Constants
.EMPTY);
String countryCode = currentPreferenceValue.equals(Constants.EMPTY) ? Locale.getDefault().getCountry() : currentPreferenceValue;
Locale loc = new Locale(Constants.EMPTY, countryCode);
return loc.getDisplayCountry();
}
****************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment