Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localized country names from Java's built-in Locale object. #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
14 changes: 11 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties

# Eclipse project files
.classpath
.project
.settings

# Android Studio
.gradle/
.idea/
build/
*.iml
local.properties

# MacOS
.DS_Store
17 changes: 0 additions & 17 deletions CountryPicker/AndroidManifest.xml

This file was deleted.

24 changes: 24 additions & 0 deletions CountryPicker/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 9
targetSdkVersion 27
versionCode 3
versionName '1.2.0'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

dependencies {
api 'com.android.support:support-fragment:27.1.1'
}
Binary file removed CountryPicker/libs/android-support-v4.jar
Binary file not shown.
20 changes: 0 additions & 20 deletions CountryPicker/proguard-project.txt

This file was deleted.

15 changes: 0 additions & 15 deletions CountryPicker/project.properties

This file was deleted.

Binary file removed CountryPicker/res/drawable-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed CountryPicker/res/drawable-xhdpi/ic_launcher.png
Binary file not shown.
5 changes: 0 additions & 5 deletions CountryPicker/res/values/strings_countries.xml

This file was deleted.

9 changes: 9 additions & 0 deletions CountryPicker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.countrypicker" >

<application
android:allowBackup="true"
android:theme="@style/AppTheme" >
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CountryListAdapter extends BaseAdapter {
* http://stackoverflow.com/
* questions/3042961/how-can-i-get-the-resource-id-of
* -an-image-if-i-know-its-name
*
*
* @param drawableName
* @return
*/
Expand All @@ -46,7 +46,7 @@ private int getResId(String drawableName) {

/**
* Constructor
*
*
* @param context
* @param countries
*/
Expand All @@ -60,19 +60,16 @@ public CountryListAdapter(Context context, List<Country> countries) {

@Override
public int getCount() {
// TODO Auto-generated method stub
return countries.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
public long getItemId(int position) {
return 0;
}

Expand All @@ -87,7 +84,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
cell = new Cell();
cellView = inflater.inflate(R.layout.row, null);
cellView = inflater.inflate(R.layout.row, parent, false);
cell.textView = (TextView) cellView.findViewById(R.id.row_title);
cell.imageView = (ImageView) cellView.findViewById(R.id.row_icon);
cellView.setTag(cell);
Expand All @@ -106,7 +103,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

/**
* Holder for the cell
*
*
*/
static class Cell {
public TextView textView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.countrypicker;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -54,9 +49,15 @@ public class CountryPicker extends DialogFragment implements
*/
private CountryPickerListener listener;

/**
* Use collator to sort locale-aware.
*/
private static final Collator sCollator = Collator.getInstance();


/**
* Set listener
*
*
* @param listener
*/
public void setListener(CountryPickerListener listener) {
Expand All @@ -74,7 +75,7 @@ public ListView getCountryListView() {
/**
* Convenient function to get currency code from country code currency code
* is in English locale
*
*
* @param countryCode
* @return
*/
Expand All @@ -89,26 +90,18 @@ public static Currency getCurrencyCode(String countryCode) {

/**
* Get all countries with code and name from res/raw/countries.json
*
*
* @return
*/
private List<Country> getAllCountries() {
if (allCountriesList == null) {
try {
allCountriesList = new ArrayList<Country>();

// Read from local file
String allCountriesString = readFileAsString(getActivity());
Log.d("countrypicker", "country: " + allCountriesString);
JSONObject jsonObject = new JSONObject(allCountriesString);
Iterator<?> keys = jsonObject.keys();

// Add the data to all countries list
while (keys.hasNext()) {
String key = (String) keys.next();
for (String cc : Locale.getISOCountries()) {
Country country = new Country();
country.setCode(key);
country.setName(jsonObject.getString(key));
country.setCode(cc);
country.setName(new Locale("", cc).getDisplayCountry());
allCountriesList.add(country);
}

Expand All @@ -129,24 +122,9 @@ private List<Country> getAllCountries() {
return null;
}

/**
* R.string.countries is a json string which is Base64 encoded to avoid
* special characters in XML. It's Base64 decoded here to get original json.
*
* @param context
* @return
* @throws java.io.IOException
*/
private static String readFileAsString(Context context)
throws java.io.IOException {
String base64 = context.getResources().getString(R.string.countries);
byte[] data = Base64.decode(base64, Base64.DEFAULT);
return new String(data, "UTF-8");
}

/**
* To support show as dialog
*
*
* @param dialogTitle
* @return
*/
Expand All @@ -165,7 +143,7 @@ public static CountryPicker newInstance(String dialogTitle) {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate view
View view = inflater.inflate(R.layout.country_picker, null);
View view = inflater.inflate(R.layout.country_picker, container, false);

// Get countries from the json
getAllCountries();
Expand Down Expand Up @@ -232,7 +210,7 @@ public void afterTextChanged(Editable s) {
/**
* Search allCountriesList contains text and put result into
* selectedCountriesList
*
*
* @param text
*/
@SuppressLint("DefaultLocale")
Expand All @@ -254,7 +232,7 @@ private void search(String text) {
*/
@Override
public int compare(Country lhs, Country rhs) {
return lhs.getName().compareTo(rhs.getName());
return sCollator.compare(lhs.getName(), rhs.getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="@string/search"
android:drawableLeft="@drawable/search_icon" />
android:drawableLeft="@drawable/search_icon"
android:drawableStart="@drawable/search_icon" />

<ListView
android:id="@+id/country_picker_listview"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:contentDescription="@string/Flag"
android:padding="10dp" />

<TextView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>

<string name="app_name">CountryPicker</string>
<string name="search">Search&#8230;</string>
<string name="Flag">Flag</string>

</resources>
Binary file removed CountryPickerExample/ic_launcher-web.png
Diff not rendered.
20 changes: 0 additions & 20 deletions CountryPickerExample/proguard-project.txt

This file was deleted.

15 changes: 0 additions & 15 deletions CountryPickerExample/project.properties

This file was deleted.

11 changes: 0 additions & 11 deletions CountryPickerExample/res/values-v11/styles.xml

This file was deleted.

Loading