Thursday, May 22, 2014

Display available currencies java.util.Currency

This example list available currencies java.util.Currency. Please notice that the methods getAvailableCurrencies() and getDisplayName() used in this example introduced in API Level 19. So android:minSdkVersion in AndroidManifest.xml have to be set ="19".


package com.example.androidcurrency;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

 ListView listCurrency;
 Set<Currency> availableCurrenciesSet;
 List<Currency> availableCurrenciesList;
 ArrayAdapter<Currency> adapter;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listCurrency = (ListView)findViewById(R.id.currencylist);
  
  //available from API Level 19
  availableCurrenciesSet = 
   Currency.getAvailableCurrencies();
  
  availableCurrenciesList = new ArrayList<Currency>(availableCurrenciesSet);
  adapter = new ArrayAdapter<Currency>(
    this, 
    android.R.layout.simple_list_item_1, 
    availableCurrenciesList);
  listCurrency.setAdapter(adapter);
  
  listCurrency.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    Currency currency = (Currency) parent.getItemAtPosition(position);
    String currencyCode = currency.getCurrencyCode();
    String displayName = currency.getDisplayName();
    String symbol = currency.getSymbol();
    
    Toast.makeText(MainActivity.this, 
     displayName + "\n" +
     currencyCode + "\n" +
     symbol, 
     Toast.LENGTH_LONG).show();
   }});
 }


}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidcurrency.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <ListView 
        android:id="@+id/currencylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>



Related:
- Display currency symbols, hard coded.

2 comments:

eriq said...

wow...thanks alot...this has been of great help

but now how can i do the same with api level below 19 ?

Anonymous said...

Thank you, thank you!! I wasn't aware of the getAvailableCurrencies() method, your post was very helpful!