Tuesday, April 15, 2014

Display currency symbols

This example TRY to show various available currency symbols on Android. This symbols reference to the file http://www.unicode.org/charts/PDF/U20A0.pdf, it contains an excerpt from the character code tables and list of character names for The Unicode Standard, Version 6.3.

Please note that some symbols cannot be shown, because it have not been installed in Android system.


MainActivity.java
package com.example.androidshowcurrency;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {
 
 MyCurrency[] MyCurrencyAll ={
   new MyCurrency("$", "dollar sign"),
   new MyCurrency("¢", "cent sign"),
   new MyCurrency("£", "pound sign"),
   new MyCurrency("¤", "currency sign"),
   new MyCurrency("¥", "yen sign"),
   new MyCurrency("ƒ", "latin small letter f with hook"),
   new MyCurrency("", "afghani sign"),
   new MyCurrency("৲", "bengali rupee mark"),
   new MyCurrency("૱", "gujarati rupee sign"),
   new MyCurrency("௹", "tamil rupee sign"),
   new MyCurrency("฿", "thai currency symbol baht"),
   new MyCurrency("¤", "khmer currency symbol riel"),
   new MyCurrency("ℳ", "script capital m"),
   new MyCurrency("元", "cjk unified ideograph-5143"),
   new MyCurrency("円", "cjk unified ideograph-5186"),
   new MyCurrency("圆", "cjk unified ideograph-5706"),
   new MyCurrency("圓", "cjk unified ideograph-5713"),
   new MyCurrency("", "rial sign"),
   new MyCurrency("₠", "EURO-CURRENCY SIGN"),
   new MyCurrency("₡", "COLON SIGN"),
   new MyCurrency("₢", "CRUZEIRO SIGN"),
   new MyCurrency("₣", "FRENCH FRANC SIGN"),
   new MyCurrency("₤", "LIRA SIGN"),
   new MyCurrency("₥", "MILL SIGN"),
   new MyCurrency("₦", "NAIRA SIGN"),
   new MyCurrency("₧", "PESETA SIGN"),
   new MyCurrency("₨", "RUPEE SIGN"),
   new MyCurrency("₩", "WON SIGN"),
   new MyCurrency("₪", "NEW SHEQEL SIGN"),
   new MyCurrency("₫", "DONG SIGN"),
   new MyCurrency("€", "EURO SIGN"),
   new MyCurrency("₭", "KIP SIGN"),
   new MyCurrency("₮", "TUGRIK SIGN"),
   new MyCurrency("₯", "DRACHMA SIGN"),
   new MyCurrency("₰", "GERMAN PENNY SIGN"),
   new MyCurrency("₱", "PESO SIGN"),
   new MyCurrency("₲", "GUARANI SIGN"),
   new MyCurrency("₳", "AUSTRAL SIGN"),
   new MyCurrency("₴", "HRYVNIA SIGN"),
   new MyCurrency("₵", "CEDI SIGN"),
   new MyCurrency("₶", "LIVRE TOURNOIS SIGN"),
   new MyCurrency("₷", "SPESMILO SIGN"),
   new MyCurrency("₸", "TENGE SIGN"),
   new MyCurrency("₹", "INDIAN RUPEE SIGN"),
   new MyCurrency("₺", "TURKISH LIRA SIGN")
  };
 
 Spinner spinnerCurrency;
 TextView textBigCurrency;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textBigCurrency = (TextView)findViewById(R.id.bigcurrency);
  spinnerCurrency = (Spinner)findViewById(R.id.spinnerCurrency);
  
  MySpinnerAdapter adapterCurr = 
    new MySpinnerAdapter(MainActivity.this, 
      R.layout.row, 
      MyCurrencyAll);
  spinnerCurrency.setAdapter(adapterCurr);
  spinnerCurrency.setOnItemSelectedListener(onItemSelectedListener);

 }
 
 OnItemSelectedListener onItemSelectedListener =
  new OnItemSelectedListener(){

  @Override
  public void onItemSelected(AdapterView<?> parent, 
    View view, int position, long id) {
   MyCurrency curr = (MyCurrency)(parent.getItemAtPosition(position));
   textBigCurrency.setText(String.valueOf(curr.getSymbol())); 
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {}

 };

 // define our custom class
 public class MyCurrency {

  private String symbol;
  private String desc;

  public MyCurrency(String symbol, String desc) {
   this.symbol = symbol;
   this.desc = desc;
  }


  public String getSymbol() {
   return this.symbol;
  }

  public String getDesc() {
   return this.desc;
  }
 }

 // custom adapter
 public class MySpinnerAdapter extends ArrayAdapter<MyCurrency> {

  private MyCurrency[] myCurrencyArray;

  public MySpinnerAdapter(Context context, int textViewResourceId,
    MyCurrency[] myObjs) {
   super(context, textViewResourceId, myObjs);
   this.myCurrencyArray = myObjs;
  }

  public int getCount() {
   return myCurrencyArray.length;
  }

  public MyCurrency getItem(int position) {
   return myCurrencyArray[position];
  }

  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(final int position, View convertView,
    ViewGroup parent) {
   return getCustomView(position, convertView, parent);
  }

  @Override
  public View getDropDownView(int position, View convertView,
    ViewGroup parent) {
   return getCustomView(position, convertView, parent);
  }

  private View getCustomView(int position, View convertView,
    ViewGroup parent) {
   LayoutInflater inflater = getLayoutInflater();
   View view = inflater.inflate(R.layout.row, parent, false);

   TextView textSymbol = (TextView) view
     .findViewById(R.id.textSymbol);
   textSymbol.setText(myCurrencyArray[position].getSymbol());
   TextView textDesc = (TextView) view
     .findViewById(R.id.textDesc);
   textDesc.setText(myCurrencyArray[position].getDesc());

   return view;
  }
 }

}

/res/layout/row.xml
<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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp" >
    
    <TextView 
        android:id="@+id/textSymbol"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView 
        android:id="@+id/textDesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic" />
</LinearLayout>

/res/layout/activity_main.xml
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidspinnertext.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" />
    
    <Spinner
        android:id="@+id/spinnerCurrency"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/bigcurrency"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:textSize="150sp" />
    
</LinearLayout>


download filesDownload the files.

Or, download the APK HERE.

Related:
Display available currencies java.util.Currency

2 comments:

  1. where we get the naira currency sysmbols

    ReplyDelete
  2. hello Ajay patidar,

    You can copy the character '₦' and paste on your soure code, as shown in the code list. Check the video at 1:08.

    ReplyDelete