A simple library that displays a beautiful list of all the currencies allowing the user to pick the currency she wishes and provide details like country code, iso code name, symbol and flag.
@midorikocak
Thanks a lot for this nice library. I have some ideas for improvements or features that i would like to have:
show optional currency symbol, e.g. on right side of row item.
mark selected item on opening with scrolling to its position. Idea is to send an selected currencyCode to the CurrencyPicker.newinstance function, after the dialog open, this currency is in focus and marked with a diffrent background color (color should be customizeable or using material seletcedbackground)
using countries instead of currencies. Pro is that you can search for example after germany, france, etc. and the result will be EUR. For this you can use Locale.getAvailableLocales(). By iterating through this list you can use Currency.getInstance(loc).getSymbol() to get Iso-Code for currencySymbol by countryCode. Here is an example of my custom CurrencyItem class which shows how this could work:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Locale;
public class CurrencyItem {
private final static String logTAG = CurrencyItem.class.getName() + ".";
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
private String symbol;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
private String countryCode;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
private String currencyCode;
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
private CurrencyItem(String country, String symbol, String countryCode, String currencyCode) {
this.country = country;
this.symbol = symbol;
this.countryCode = countryCode;
this.currencyCode = currencyCode;
}
public CurrencyItem(String countryCode) {
Locale loc = new Locale("", countryCode);
this.country = loc.getDisplayCountry();
this.symbol = Currency.getInstance(loc).getSymbol();
this.countryCode = loc.getCountry();
this.currencyCode = Currency.getInstance(loc).getCurrencyCode();
}
public CurrencyItem() {
Locale loc = Locale.getDefault();
this.country = loc.getDisplayCountry();
this.symbol = Currency.getInstance(loc).getSymbol();
this.countryCode = loc.getCountry();
this.currencyCode = Currency.getInstance(loc).getCurrencyCode();
}
public static ArrayList<CurrencyItem> listDefault() {
ArrayList<Locale> localeList = new ArrayList<>();
localeList.add(Locale.getDefault());
localeList.add(Locale.GERMANY);
localeList.add(Locale.US);
localeList.add(Locale.UK);
localeList.add(Locale.JAPAN);
localeList.add(Locale.CHINA);
return getCurrencyList(localeList);
}
public static ArrayList<CurrencyItem> listAll() {
ArrayList<Locale> localeList = new ArrayList<>();
localeList.add(Locale.getDefault());
localeList.add(Locale.GERMANY);
localeList.add(Locale.US);
localeList.add(Locale.UK);
localeList.add(Locale.JAPAN);
localeList.add(Locale.CHINA);
ArrayList<Locale> allLocals = new ArrayList<>(Arrays.asList(Locale.getAvailableLocales()));
Collections.sort(allLocals, new Comparator<Locale>() {
@Override
public int compare(Locale share, Locale share2) {
return share.getDisplayCountry().compareTo(share2.getDisplayCountry());
}
});
localeList.addAll(allLocals);
return getCurrencyList(localeList);
}
private static ArrayList<CurrencyItem> getCurrencyList(ArrayList<Locale> localeList) {
ArrayList<CurrencyItem> list = new ArrayList<>();
for (Locale loc : localeList) {
try {
String curCode = loc.getDisplayCountry();
String curSymbol = Currency.getInstance(loc).getSymbol();
CurrencyItem item = new CurrencyItem(curCode, curSymbol, loc.getCountry(), Currency.getInstance(loc).getCurrencyCode());
if (!list.contains(item)) {
list.add(item);
// Log.v(logTAG + " test", String.format("%s (%s), %s, %s", loc.getDisplayCountry(), Currency.getInstance(loc).getSymbol(), item.getCountryCode(), item.getCurrencyCode()));
}
} catch (Exception exc) {
// Locale not found
}
}
return list;
}
@Override
public boolean equals(Object obj) {
return obj instanceof CurrencyItem && countryCode.equals(((CurrencyItem) obj).countryCode);
}
@Override
public int hashCode() {
return (countryCode == null) ? 0 : countryCode.hashCode();
}
}
use a recyclerView instead of a listView beacause of better performance
offering a fragmentView instead of dialog. So its easier to customize the style of your lib to the app style or to implement your libraray in any view.
not hard coded currencyNames, better using strings.xml to give option for localiziation
@midorikocak Thanks a lot for this nice library. I have some ideas for improvements or features that i would like to have:
Could you please implement these features?
Regards Scrounger