0

I made a simple currency converter, but is there a simple way to retrieve live currency (just the currency, say from Israel shekel to dollar)? For example:

case R.id.euro:
        mDollar.setChecked(false);
        Meuro.setChecked(true);
        exchangeRate = **//live currency from external source**
        exchangeSymbol = "€";

        break;

Here is my whole code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mResult = (TextView) findViewById(R.id.result);
    mToConvert = (EditText) findViewById(R.id.toConvert);
    mRadioGroup = (RadioGroup) findViewById(R.id.radioG);
    mDollar = (RadioButton) findViewById(R.id.dollar);
    Meuro = (RadioButton) findViewById(R.id.euro);

    mToConvert.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            convertCurrentAmount();

        }
    });
    mRadioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    convertCurrentAmount();
                }
            });
}

public void convertCurrentAmount() {
    double exchangeRate = -1;
    String exchangeSymbol = null;
    switch (mRadioGroup.getCheckedRadioButtonId()) {
    case R.id.dollar:
        mDollar.setChecked(true);
        Meuro.setChecked(false);
        exchangeRate = 3.76;
        exchangeSymbol = "$";


        break;

    case R.id.euro:
        mDollar.setChecked(false);
        Meuro.setChecked(true);
        exchangeRate = 5;
        exchangeSymbol = "€";

        break;
    }
    if (exchangeRate > 0 && exchangeSymbol != null) {

        Double stringtoint = Double
                .valueOf(mToConvert.getText().toString());
        double result = stringtoint * exchangeRate;
        mResult.setText("" + exchangeSymbol + result);

    }
}

}

Khantahr
  • 8,156
  • 4
  • 37
  • 60
Yonatan Balas
  • 35
  • 1
  • 9

1 Answers1

1

The European Central Bank has a site for daily currency rates in XML.

http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

Some more info here: http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html#dev

You should probably load and parse the XML on startup and also save it for offline usage and so that you don't have to fetch the values every time(once a day is enough).

Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19
  • thanks. but i am new to programming, my question is there a possible way to get this xml and parse only the from the document? – Yonatan Balas Dec 19 '12 at 19:02
  • Everything is possible with programming so, yes of course. I would however recommend that you map it to a object for future use then resolve whatever currency you are converting with. – Nicklas Gnejs Eriksson Dec 19 '12 at 19:05
  • If you are only going to convert one currency (ILS) then Marvins solution might be better. But if you are doing a converter where you can choose to and from currencies then this one is better, especially since it does not require a internet connection after a successful parsing. – Nicklas Gnejs Eriksson Dec 19 '12 at 19:09