-AUD (Australian Dollar)
-CAD (Canadian Dollar)
-CHF (Swiss Franc)
-EUR (Euro)
-GBP (British Pound)
-JPY (Japnese Yen)
-NZD (New Zealand Dollar)
-KHR (Khmer Riel)
-USD (American Dollar)
-CNY (Chinese Yuan Renminbi)
-THB (Thai Baht)
-INR (Indian Rupee)
To follow this tutorial, you will need to create a new Android project in Eclipse. The project will be named CurrencyConverter.
In the CurrencyConverter app, the user is allowed to select a pair of currency for the conversion. One is the currency that will be converted from and another is the currency that will be converted to. We also allow the user to input the exchanged rate and the amount to be converted. One button is provided to show the table of conversion. The table displays the currency types and amounts. The currency types that are not the target of the conversion, their amounts are calculated based on their default average exchange rates. For target currency type, its amount is calculated based on the exchange rate provided by the user. The target currency is highlighted.
In this CurrencyConverter app, there are two activities. On activity is the main activity that starts when the app initially runs. The components or view that are used to construct the user interface of the main activity are two TextViews, two Spinner, two EditTexts, and one Button. The two TextViews to display the labels of the Spinners. The two Spinners allow the user to select a currency type to convert from and another currency type to converted to. The spinner_style.xml file defines the item style of the Spinners. One EditText is for the user to input the exchange rate and another one allows the user to input the amount for converting. The Show button will be pushed to display the conversion table. This button has background style that defined by the bt_style.xml file. The activity_main.xml file that is the resource of main activity is written as shown below.
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: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"
android:background="#ff8899"
tools:context=".MainActivity" >
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/from_currency"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="@string/from_currency" />
<Spinner
android:id="@+id/fromcurrency_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/to_currency"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="@string/to_currency" />
<Spinner
android:id="@+id/tocurrency_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<EditText
android:id="@+id/txt_rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_rate"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/txt_fromamount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_fromamount"
android:inputType="numberDecimal" />
<Button
android:id="@+id/bt_show"
android:background="@drawable/bt_style"
android:text="@string/bt_label"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:onClick="showResult"
/>
</LinearLayout>
spinner_style.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
/>
bt_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#ff00ff" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#992211"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
The main activity is defined by the MainActivity class. Besides loading the components from the activity_main.xml file, the MainActivity has code to set up the item data of the Spinners. This task is done by calling the setupSpinnerData method. To capture the From currency and To currency, the two Spinners has to register to the items selected event. When the user selected a currency from the first Spinner, the selected currency is stored in the fromCurrency variable. Similarly, the selected currency from the second Spinner is stored in the toCurrency variable.
The showResult result method is invoked when the user pushes the Show button. Before showing the conversion table, the app checks to make sure that the exchange rate text box and the amount text are not blank. The alert dialog box is displayed when he/she tries to click the Show button with the blank exchange rate or amount. The intent object is created to put some data such as From currency, To currency, exchange rate, and amount. The intent object that contains the data is sent to the second activity after the startActivity method is invoked. Below is the content of the MainActivity.java file that has the MainActivity class.
MainActivity.java
package com.example.currencyconverter;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private String fromCurrency;
private String toCurrency;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onResume(){
super.onResume();
setUpSpinnerData();
}
//This method will be invoked to setup data of the spinner views
//to show lists of currency types for selection
public void setUpSpinnerData(){
Spinner spFrom=(Spinner)findViewById(R.id.fromcurrency_spin);
Spinner spTo=(Spinner)findViewById(R.id.tocurrency_spin);
String[] currencyList={"AUD","CAD","CHF","EUR","GBP","JPY","NZD","KHR","USD","CNY","THB","INR"};
ArrayAdapter<String> afrom=new ArrayAdapter<String>(this,R.layout.spinner_style,currencyList);
spFrom.setAdapter(afrom);
spFrom.setOnItemSelectedListener(new ItemSelectedFrom());
ArrayAdapter<String> ato=new ArrayAdapter<String>(this,R.layout.spinner_style,currencyList);
spTo.setAdapter(ato);
spTo.setOnItemSelectedListener(new ItemSelectedTo());
}
private class ItemSelectedFrom implements OnItemSelectedListener{
public void onNothingSelected(AdapterView<?> av){
}
public void onItemSelected(AdapterView<?> av, View view, int position, long id){
TextView sel=(TextView)view;
String from=sel.getText().toString();
fromCurrency=from; //capture the currency of the From side
EditText txtfrom=(EditText)findViewById(R.id.txt_fromamount);
txtfrom.setHint("Enter "+fromCurrency+" amount");
}
}
private class ItemSelectedTo implements OnItemSelectedListener{
public void onNothingSelected(AdapterView<?> av){
}
public void onItemSelected(AdapterView<?> av, View view, int position, long id){
TextView sel=(TextView)view;
String to=sel.getText().toString();
toCurrency=to; //capture the currency of the To side
}
}
public void showResult(View view){
EditText txtRate=(EditText)findViewById(R.id.txt_rate);
EditText txtAmount=(EditText)findViewById(R.id.txt_fromamount);
if(txtRate.getText().toString().length()<=0 || txtAmount.getText().toString().length()<=0){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please input value in text box.");
builder.setCancelable(true);
builder.setPositiveButton("OK", new OnClickListener(){
public void onClick(DialogInterface di,int which){
di.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else{
//create intent, place data in it and start the ConversionTable activity
Intent intent=new Intent(this, ConversionTable.class);
intent.putExtra("fromCurrency", fromCurrency);
intent.putExtra("toCurrency", toCurrency);
intent.putExtra("Rate", Double.valueOf(txtRate.getText().toString()));
intent.putExtra("fromAmount", Double.valueOf(txtAmount.getText().toString()));
startActivity(intent);
}
}
}
Another activity is called ConversionTable. It shows when the user pushes the Show button. This activity displays the conversion table as i mentioned above. This activity has a resource xml file called activity_conversion_tabl.xml. This file defines one ListView component to show the conversion table.
activity_conversion_tabl.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: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=".ConversionTable" >
<ListView
android:id="@+id/conversion_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
When constructing the conversion table, you will need the icon images of the countries that their currency types are used in the CurrencyConverter app. You can download the icon images from here.
In default, ListView in Android displays on text. However, this CurrencyConverter app, the conversion table has to display icon images, currency types, and amount text. We have to customize the ListView to do this task. To customize the ListView, you start from defining the components for its item. The ListView has its own layout file called listlayout.xml that defines the layout of its item or row. The content of the listlayout.xml file is show below.
listlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000"
android:padding="5dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="5sp"
android:contentDescription="countryicon"
/>
<TextView
android:id="@+id/currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10sp"
android:textSize="20sp"
android:textColor="#ffffff"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/label_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10sp"
android:textSize="20sp"
android:textColor="#ffffff"
android:textStyle="bold" >
</TextView>
</LinearLayout>
Next, you need to extends the ArrayAdapter class that acts as the data source of the ListView. This class is called ListAdapterModel. Its getView method is overridden to allow the ListView to display icon image, currency type, and amount.
ListAdapterModel.java
package com.example.currencyconverter;
import java.text.DecimalFormat;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapterModel extends ArrayAdapter<String>{
int groupid;
String[] clist;
Context context;
String to;
Double[] alist;
public ListAdapterModel(Context context, int vg,String[] clist, Double[] alist, String to){
super(context,vg, clist);
this.context=context;
groupid=vg;
this.clist=clist;
this.to=to;
this.alist=alist;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
imageView.setImageDrawable(context.getResources().getDrawable(getImage(position)));
TextView textCurrency = (TextView) itemView.findViewById(R.id.currency);
TextView textAmount= (TextView) itemView.findViewById(R.id.label_amount);
textCurrency.setText(clist[position]);
DecimalFormat df=new DecimalFormat("#,###.0000");
String value=df.format(Double.valueOf(alist[position]));
textAmount.setText(value);
if(clist[position].equals(to)) //highlight the target conversion row
itemView.setBackgroundColor(Color.MAGENTA);
return itemView;
}
public Integer getImage(int pos){
Integer[] imageIds=new Integer[12];
imageIds[0]=R.drawable.aud;
imageIds[1]=R.drawable.cad;
imageIds[2]=R.drawable.chf;
imageIds[3]=R.drawable.eur;
imageIds[4]=R.drawable.gbp;
imageIds[5]=R.drawable.jpy;
imageIds[6]=R.drawable.nzd;
imageIds[7]=R.drawable.khr;
imageIds[8]=R.drawable.usd;
imageIds[9]=R.drawable.cny;
imageIds[10]=R.drawable.thb;
imageIds[11]=R.drawable.inr;
return(imageIds[pos]);
}
}
Inside the ConversionTable class, Java code is written to construct the conversion table. The constructTable method is called to prepare the data for the ListView and display the currency conversion table. The data to be defined here are the array of amounts (amounts) and the currency list (clist). Each amount that is the item of the amounts array is calculated based on this formula:
amounts[i]=rate*amount;
The default average exchange rates are used for the currency types that are not the target. For the target conversion currency, the rate input by the user will be used. The clist array contains all currency types that are supported by the CurrencyConverter app.
The amounts array is used by the ListAdapterModel class to display the amounts and the clist array contains the currency types to show with the amount on the conversion table. The icon image list is not defined here. It is in the ListAdapterModel class.
The AndroidManifest.xml file is modified to allow the second activity to be the child of the main activity. When you are on the second activity and press the back button, you will come back to the main activity. The content of the AndroidManifest.xml file is shown below.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.currencyconverter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/currencyconverter"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.currencyconverter.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.currencyconverter.ConversionTable"
android:label="@string/title_activity_conversion_table"
android:configChanges="orientation"
android:parentActivityName="com.example.currencyconverter.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.currencyconverter.MainActivity" />
>
</activity>
</application>
</manifest>
Now you are ready to run the program. If you have any questions, please write them at the comment section. I will reply as soon as possible. Thank you for reading this post.
como transforma isso tudo em arquivos apk, para fazer a instalação no andoid?
ReplyDeletethere is no ConversionTable .java code
ReplyDeleteHi this one is great and is really a good post. I think it will help me a lot in the related stuff
ReplyDeleteGreat article ...Thanks for your lovely post, the contents are quiet interesting. I will be waiting for your next post.
Image Icon Converter
project run but cannot display second screen
ReplyDeleteor output
con not display conversion table
ReplyDeletesend me solution
very nice app, but please, can you write the ConversionTable.java code?
ReplyDeleteThank you!
Thank you so much for your tutorial. I've created my first currency converter: https://play.google.com/store/apps/details?id=com.nikitadev.currencyconverter
ReplyDeleteI need ur help for making currency converter plz reply me fast
DeleteI need ur help for making currency converter plz reply me fast
DeleteThank you so much for your tutorial. How would it work as unit converter for measurements ?
ReplyDeletevery good for my new app
ReplyDeletehi...
ReplyDeleteThere is no ConversionTable class plz send me this class..
give full code correctly
ReplyDeletePlease provide the complete code of conversion table
ReplyDeleteHow can we join two tables in sqlite and display merged data in another activity anybody know how if know please give me a complete code for that.....urgently need please help
ReplyDeletethanks a lot sir. more power and God bless
ReplyDeleteHi! Oh this is gonna be useful for me :) thank you
ReplyDeletecan u plzz send proper code of conversion table and listadapter
ReplyDeleteNew york: Although your Tar High heels lost inside the ACC match semifinals, one of several Duke on the rules for a lot of the initial one half. Justin Knutson, who has got developed your good edge gameplay, and a foul-free Joel Berries II are the keys.
ReplyDeleteMarch Madness
March Madness Live
March Madness Live Stream
March Madness 2017
Absolutely, small children Recognise Small number of has not built one final Some. This power team, filled using proficient scorers that will as well locking mechanism off opponents, is going to be her first
March Madness 2017 Live
ncaa March Madness
ncaa march madness live
ncaa tournament
March Madness Bracket
ncaa final four
No matter whether you’re always over the fencing that 8-9 sport, or are seeking a total opinion on your Very last Some selects, we’ve bought everyone taken care of which has a position of their 68 clubs in the event. Anybody can together with generate the one you select upon hundreds of mid-major darlings you’ve already been eyeing designed for worried potential.
March Madness
March Madness Live
March Madness Live Stream
March Madness 2017
March Madness 2017 Live
ncaa March Madness
ncaa march madness live
ncaa tournament
March Madness Bracket
ncaa final four
Villanova: The superior general seedling. Despite the team’s placement in the hardest location (the East, using matchups vs Duke along with SMU looming), Villanova’s three-man unpleasant heart involving Jalen Brunson, Josh Hart along with Kris Jenkins offers the potential to help you repeat as indigenous winners.
nzaga's defensive lapses in those final minutes. Against 16-seed SDSU, the Bulldogs led by only four points at halftime and made just 31 percent of its shots in the first half of a first-round win. They beat SDSU by 20 points and Northwestern by six points. The Bulldogs advanced. That's what matters. They did not move forward via their most impressive performances of the season.
ReplyDeleteMarch Madness
March Madness Live
March Madness Live Stream
March Madness 2017
March Madness 2017 Live
ncaa March Madness
ncaa march madness live
ncaa tournament
March Madness Bracket
ncaa final four
week after Michigan's plane flew off the runway prior to a surprising Big Ten tournament championship run, the Wolverines will enter the week as a legit threat to reach the Final Four in a Midwest Region that seems perfect for a team like coach John Beilein's. The Wolverines registered 92 points against an
http://spottingscope.spruz.com/
ReplyDeletehttp://spottingscope.spruz.com/
http://spottingscope.spruz.com/
http://spottingscope.spruz.com/
http://spottingscope.spruz.com/
Valuable information in your blog and I really appreciate your work and keep it up dude I really very informative blog about the Fast Currency Converter App keep posting.
ReplyDeleteFast Currency Converter App
Essentially the most actively traded crosses pinpoint the three major non-USD currencies (namely EUR, JPY, and GBP) and are also known as Euro crosses, yen crosses, plus the sterling crosses.
ReplyDeleteguarantor loans
Vmware Training in Chennai
ReplyDeleteCCNA Training in Chennai
Angularjs Training in Chennai
Google CLoud Training in Chennai
Red Hat Training in Chennai
Linux Training in Chennai
Rhce Training in Chennai
You are great with words. I enjoyed this and I will be back for more.Buy Gold Coins In San Francisco
ReplyDeleteandroid:textColor="#ffffff"
ReplyDeleteandroid:textSize="18sp"
android:text="@string/to_currency" />
<Spinner
android:id="@+id/tocurrency_spin"
android:layout_width="wrap_content"
The blog was absolutely fantastic! Lot of great information which can be in some helpful or the other way. Keep updating the blog, looking forward for more happy...Great job, keep it up...
ReplyDeleteSEO, SMM, SMO, SEM, Web Designing Training in Chennai
Digital Marketing Course in Chennai
Digital marketing training institute in Chennai
Digital marketing classes in Chennai
Digital marketing training in Chennai
SEO Training Institute in Chennai
SEO Training in Chennai
SEO Classes in Chennai
SEO Course in Chennai
Best SEO Training in Chennai
SMO Training in Chennai
Social Media Marketing Institute in Chennai
SEM Training institute in Chennai
Web Designing Training in Chennai
Web Designing Classes in Chennai
Soft Skills Training in Chennai
Nice post, very useful blogs with very useful information, thank you for sharing this post web application development company
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIncludes Digital Money and Currency exchange rates in over 200 currencies letting you manage them all in a single screen; in addition the app includes historic rate charts and notifications which you can set up notifications to stay up to date with the most important Cryptocurrencies like Bitcoin, Litecoin, Ethereum, Ripple, Stellar, Cardano, Dogecoin and Much More.
ReplyDeleteDownload Now! Crypto Converter for FREE in Google Play!
ReplyDeleteHow do you clean a fabric sofa?
How much does it cost to have a sofa cleaned?
How do you clean a sofa?
Can you get a couch professionally cleaned?
شركة تنظيف كنب بمكة بالبخار
شركة تنظيف كنب بمكة بالبخار
One other issue I feel we actually want to contemplate is the latest addition of Bitcoin futures buying and selling. I personally imagine that there are main forces at work right here led by the previous guard that wish to see crypto fail. If you want to learn more about this topic please visit https://quickex.io/exchange-btc-trx
ReplyDeletesoftware testing company in India
ReplyDeletesoftware testing company in Hyderabad
Thanks for sharing such a interesting information with us.
please keep sharing.
Forex rates are the rates commonly traded between all major country's currencies. These are subject to change regularly as the trader buy and sell more or less of the currencies and the general supply and demand increases or decreases. Bitcoin price in India
ReplyDelete