How to create a simple Currency Converter App in Android Studio

Kanchana Jayasinghe
4 min readOct 5, 2019

Hi all ! Welcome to my blog on Android Studio app development. Here we go…….,

Currency Conversion……?

The process of converting one form of currency into another country’s usable currency. Based on current exchange rates, a person may receive less or more value after the currency is converted. This can be determined by looking at the current exchange rate for the country’s currency.

How to Create it — UI, Source codes and Screenshots in Android Studio

  • Open Android Studio Start MenuAll Programs to get started.
  • Once android Studio has been launched, Go to FileNewCreate a new Project and give a name to it.
  • Click Next
  • Select the Form Factors and minimum SDK (Just tick Phone and Tablet).
  • Click NextAdd an empty Activity to the Project.
  • Create a new empty ActivityFinish.
  • Then, we have to create two files, the MainActivity.java and Activity_main.xml
  • Put all our Simple Currency Converter source code in activity_main.xml and the Java implementations in MainActivity.java.

Source codes of Currency Converter App

  • Activity_main.xml Source Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignEnd="@+id/button"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:layout_marginEnd="-14dp"
android:src="@drawable/icon"
android:layout_alignRight="@+id/button"
android:layout_marginRight="-14dp"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginTop="164dp"
android:text="Euro"
/>

<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_centerHorizontal="true"

android:layout_marginBottom="-16dp"
android:ems="10"
android:inputType="number"
/>

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="231dp"
android:text="Curency"
/>

<Spinner
android:id="@+id/planets_spinner"

android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView5"
android:layout_marginLeft="70dp"
android:layout_marginTop="1dp"
/>


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="125dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="297dp"
android:text="Converte"
/>

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginBottom="113dp"
android:text="Value"
/>

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView6"
android:layout_marginStart="44dp"
android:layout_marginTop="0dp"
android:layout_toEndOf="@+id/textView5"
android:text="TextView"
/>
</RelativeLayout>
  • Code implementation in MainActivity.java
package com.example.currency_converter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


import com.example.currency_converter.R;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



public class MainActivity extends AppCompatActivity {

public static BreakIterator data;
List<String> keysList;
Spinner toCurrency;
TextView textView;

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

toCurrency = (Spinner)findViewById(R.id.planets_spinner);
final EditText edtEuroValue = (EditText)findViewById(R.id.editText4);
final Button btnConvert = (Button)findViewById(R.id.button);
textView =(TextView) findViewById(R.id.textView7);
try {
loadConvTypes();
} catch (IOException e) {
e.printStackTrace();
}

btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!edtEuroValue.getText().toString().isEmpty())
{
String toCurr = toCurrency.getSelectedItem().toString();
double euroVlaue = Double.valueOf(edtEuroValue.getText().toString());

Toast.makeText(MainActivity.this, "Please Wait..", Toast.LENGTH_SHORT).show();
try {
convertCurrency(toCurr, euroVlaue);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Please Enter a Value to Convert..", Toast.LENGTH_SHORT).show();
}

}
});

}

public void loadConvTypes() throws IOException {

String url = "https://api.exchangeratesapi.io/latest";

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.build();



client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
String mMessage = e.getMessage().toString();
Log.w("failure Response", mMessage);
Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
}

@Override
public void onResponse(Response response) throws IOException {
final String mMessage = response.body().string();


MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
try {
JSONObject obj = new JSONObject(mMessage);
JSONObject b = obj.getJSONObject("rates");

Iterator keysToCopyIterator = b.keys();
keysList = new ArrayList<String>();

while(keysToCopyIterator.hasNext()) {

String key = (String) keysToCopyIterator.next();

keysList.add(key);

}


ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, keysList );
toCurrency.setAdapter(spinnerArrayAdapter);





} catch (JSONException e) {
e.printStackTrace();
}

}
});
}




});
}

public void convertCurrency(final String toCurr, final double euroVlaue) throws IOException {

String url = "https://api.exchangeratesapi.io/latest";

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.build();



client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
String mMessage = e.getMessage().toString();
Log.w("failure Response", mMessage);
Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
}

@Override
public void onResponse(Response response) throws IOException {
final String mMessage = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
try {
JSONObject obj = new JSONObject(mMessage);
JSONObject b = obj.getJSONObject("rates");

String val = b.getString(toCurr);

double output = euroVlaue*Double.valueOf(val);


textView.setText(String.valueOf(output));

} catch (JSONException e) {
e.printStackTrace();
}

}
});
}





});
}
}
  • Add following part to dependencies in Gradle.
dependencies {

implementation 'com.squareup.okhttp:okhttp:2.5.0'
}

User Manual for the above mentioned app

  • Enter the amount in Euro in the provided space.
  • Choose any of currency type you want to convert above mentioned value using the drop-down list.
  • Click “Convert” Button.
  • Then the app will display the converted value below

Screenshots of User Interfaces

Basic User Interface before conversion.
User Interface after the conversion.

That’s all for today. Please try this and send me your feedback. Your valuable comments are warmly welcome. We’ll meet from another lesson.

Thank you for reading.. : )

--

--

Kanchana Jayasinghe

Software Engineering Intern @ WSO2, Undergraduate of Uva Wellassa University