Currency Converter Application for Android
This tutorial will give you a brief introduction to developing a GUI application on Android. This tutorial assumes that you have downloaded and installed the Android SDK. This tutorial also assumes that you are reasonably familiar with concepts in GUI programming and the Java programming language. The complete project can be found at end of post
Step One:
In Eclipse create a new android project as CurrencyConverter. Open the contents of
res/layout/main.xml.Delete everything except <?xml version=“1.0″ encoding=“utf-8″?> from the main.xml file.
Step Two:
Select the “Graphical Layouts” tab.
Step Three:
Drag and drop a Relative Layout object from the Layouts panel into the screen and click on the screen. You will see the following things:
Step Four:
Drag and drop a Linear Layout object from the Layouts panel into the screen top border line .
Step Five:
Select the Linear Layout object and click on the properties tab to begin editing the layout properties. Change the width to “200px” and the height to “280px”and Orientation as vertical.
Step Six:
Drag and drop two TextView objects and two EditText objects into the LinearLayout so that they alternate
Step Seven:
Edit the properties of each TextView object. Make text for the upper one read “Dollars” and make its style “bold”. Make the lower one read: “Euros” and make its style bold also.
Step Eight:
Edit the properties of the upper EditText as follows:
- Change the id to read: “@+id/dollars”
- Change the text to be empty
- Change the width to be “100px”.
- Change the height to be “50px”
- Add lines as “1”
Step Nine:
- Repeat step Eight with the second EditText under the “Euros” TextView, but make the id be “@+id/euros”
Step Ten:
Drag and drop a RadioGroup object into the LinearLayout. Drag and drop two RadioButton objects into the RadioGroup.
Step Eleven:
Edit the first RadioButton so that its text reads: “Dollars to Euros” and its id is “@+id/dtoe”.
Edit the second RadioButton so that its text reads: “Euros to Dollars” and its id is “@+id/etod”.
Edit the second RadioButton so that its text reads: “Euros to Dollars” and its id is “@+id/etod”.
Step Twelve:
Drag and drop a Button object into the root RelativeLayout below the LinearLayout object. It should align with the right edge of the LinearLayout.
Important Note:
You must get the ids exactly correct, because this is how you will look up the widgets in source code.
You must get the ids exactly correct, because this is how you will look up the widgets in source code.
Step Thirteen:
The main.xml file will look something like this.
<?xml version=“1.0″ encoding=“utf-8″?>
<RelativeLayout android:id=“@+id/RelativeLayout01″
android:layout_width=“fill_parent” android:layout_height=“fill_parent”
xmlns:android=“http://schemas.android.com/apk/res/android”>
<LinearLayout android:layout_alignParentTop=“true” android:layout_width=“200px”android:orientation=“vertical” android:layout_height=“280px”android:id=“@+id/linearlayoutId”><TextView android:id=“@+id/TextView01″android:layout_width=“wrap_content” android:layout_height=“wrap_content”android:text=“Dollars” android:textStyle=“bold”></TextView>
<EditText android:layout_width=“100px” android:layout_height=“50px”android:id=“@+id/dollars” android:lines=“1″></EditText>
<TextView android:id=“@+id/TextView02″ android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“Euros” android:textStyle=“bold”></TextView>
<EditText android:lines=“1″ android:layout_height=“50px” android:layout_width=“100px”android:id=“@+id/euros”></EditText>
<RadioGroup android:id=“@+id/RadioGroup01″ android:layout_width=“wrap_content”android:layout_height=“wrap_content”><RadioButton android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“Dollars to Euros”android:id=“@+id/dtoe”></RadioButton><RadioButton android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“Euros to Dollars”android:id=“@+id/etod”></RadioButton>
</RadioGroup>
</LinearLayout>
<Button android:layout_height=“wrap_content” android:layout_width=“wrap_content”android:layout_below=“@+id/linearlayoutId” android:layout_alignRight=“@+id/linearlayoutId”android:text=“Convert” android:id=“@+id/convert”></Button>
</RelativeLayout>
Step Fourteen:
At this point you should be able to run your GUI in Android. It should look something like this:
Step Fifteen:
The last step is to actually code the currency conversion. There’s not much to it, you can look up your GUI elements with:
this.findViewById(R.id.<id>).
this.findViewById(R.id.<id>).
Here is the complete code for the CurrencyConverter activity:
package com.droidbd.currencyconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class CurrencyConverter extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText dollars;
EditText euros;
RadioButton dtoe;
RadioButton etod;
Button convert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dollars = (EditText)findViewById(R.id.dollars);
euros = (EditText)findViewById(R.id.euros);
dtoe = (RadioButton)this.findViewById(R.id.dtoe);
dtoe.setChecked(true);
etod = (RadioButton)this.findViewById(R.id.etod);
convert = (Button)this.findViewById(R.id.convert);
convert.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (dtoe.isChecked()) {
convertDollarsToEuros();
}
if (etod.isChecked()) {
convertEurosToDollars();
}
}
protected void convertEurosToDollars() {
// TODO Auto-generated method stub
double val = Double.parseDouble(euros.getText().toString());
// in a real app, we’d get this off the ‘net
dollars.setText(Double.toString(val/0.67));
}
protected void convertDollarsToEuros() {
// TODO Auto-generated method stub
double val = Double.parseDouble(dollars.getText().toString());
// in a real app, we’d get this off the ‘net
euros.setText(Double.toString(val*0.67));
}
}









No comments:
Post a Comment