App Inventor Currency Converter (NO EXTENSION | Currency Converter App Inventor

Hello friends,

Welcome to Obsidian Soft

Let's make a simple but accurate Currency Converter app today.
First, we need to use a free API to get exchange rates. https://www.exchangerate-api.com/docs/overview

Click on Get Free key button and provide your email address

Check your email and go to the authentication link. Copy the FREE key. 


Remember that this free API key will have limited usage and if you want to publish this app to the Play Store, you must get a proper billing plan.

Now, click on Standard API Requests on the left under the Documentation heading:
Look at the standard response: A simple dictionary object with key-value pairs. 
You will notice that against the key: conversion_rates, we are getting another dictionary where keys are 3 lettered currencies and the value is the exchange rate relative to USD…very very easy JSON so keeping all this in mind, let’s proceed to MIT App inventor and start a new project and call it CurConverter.

I am going to make a simple screen so that I have more time to focus on the code. For screen1, in its properties, make align horizontal and align vertical both center. Make the background color white.

Drag and drop a text box from the User Interface palette onto the viewer. Rename it to amountTxt. Make font bold, font size 20 and change hint to “Enter amount:” Also, turn on the numbers only checkbox as the amount to be converted is a number.
Drag and drop a spinner from the User Interface onto the viewer below the text box. Rename it to fromSpinner.
Drag and drop another spinner below it. Rename it to toSpinner.
Drag and drop a button from the user interface below the second spinner. Rename it to convertBtn. Make the background color: orange, font bold, font size: 20, and shape rounded. Change the text on it to Convert. Drag and drop a label from the user interface below this button. Rename it to convertedAmount. Make its font bold and font size: 25. Remove all text from it.

From the Connectivity palette, drag and drop a web component onto the viewer.

Our Screen design is done so let’s go to the blocks section.

Make a global variable for our api key.

Copy the key from the exchangerate-api page.
And paste it here. Please don’t use my key. Kindly get your own.
Make a global variable for currencies. This is an empty list.

Make a global variable for exchange rates. This is an empty dictionary.

Now, get the screen1’s initialize event where we will make our api request to get data. Be very careful while constructing the link. Copy the example from the exchange rate page and break it down into 3 parts.

After setting the url for the Web component, call its Get procedure.
This call will trigger the Web’s GotText event when some response is received from the server.
First, make a local variable called decodedDict and give it the decoded json from the response content.

Now, we have to check if we get a successful response from the server. This we can know if we look at the value for the key “result” inside our decoded dictionary i.e. responseDic

If it is equal to success, we are going to get the dictionary against key: “conversion_rates” and set it to our global exchangeRates variable.

Now, here comes the interesting part:
Our exchangeRates variable is a dictionary with keys containing currencies and the corresponding values are the exchange rates. So, if we get only the keys from this dictionary, we have our list of currencies.
So, set the currencies global variable to the keys in our exchangeRates dictionary and provide this currencies list as elements to our two spinners.

If the app is used without being connected to the internet, we will get an error with code 1101.

We can handle this error gracefully by getting the screen’s error occurred event and if the code is 1101, we can disable the convert button and show an error message in our label.


Now, let’s work on our convert button but first remember that we are getting all interest rates relative to the US dollar so if the user wants to convert an amount from some other currency then we need to do some more calculations.

For example, I want to convert £50 into Saudi Riyals (SAR).
I need to first figure out how many US Dollars (USD) the British Pounds are worth.

Assume the API returns the following rates:
usdToGbp = 0.76 (1 USD = 0.76 GBP)
usdToSar = 3.75 (1 USD = 3.75 SAR)

1. Convert GBP to USD:

To find how much USD you get for 1 GBP, use: 1÷usdToGbp=1÷0.76=1.31581

This means 1 GBP = 1.3158 USD.

2. Convert USD to SAR:


Now, multiply this USD value by the usdToSar rate (since we already have the USD-to-SAR rate): 1.3158×3.75=4.9343

So, 1 GBP = 4.9343 SAR.

3. Multiply by the Amount in GBP:

If the user wants to convert £50, multiply the conversion rate by 50: 50×4.9343=246.71550

Therefore, £50 = 246.71 SAR.
So, the final formula is

convertedAmount =

(1÷usdToGbp) x usdToSar x enteredAmount

So, let’s work on the convert button. First, do some error handling that the user has entered some amount:


Next, make a local variable amount and set it to 0.
And then, use the formula explained above:
We will use the selection from the fromSpinner and use it as a key and look for the exchange rate corresponding to it in our exchange rates dictionary. This will get us our “from currency” rate. We will do same for toSpinner to get the “to currency” rate.


So, this is done. I hope you like this video. Have fun coding this app for your school or college projects or just for fun to impress your friends.

You can have a look at the video tutorial here:

Please like my videos and share them with your friends and family. Also, subscribe to my channel and press the bell icon so you don't miss any of the great projects I have planned for you.https://www.youtube.com/c/obsidiansofteducation


Please like my social media pages for more educational resources and tips.
Pinterest:
For links to free educational apps, have a look at the educational apps page



Comments

Popular posts from this blog

Jolly Phonics Group 1

Jolly Phonics Group 2

Jolly Phonics Flash Cards