Making a currency converter on Android; Part 1 - YQL and Activity - Tundra blog

Feel the cooling sensation of knowing things...


It is a programming blog btw

Hot

20 Mart 2014 PerÅŸembe

Making a currency converter on Android; Part 1 - YQL and Activity

Hi :)  This tutorial series will cover all the things to make a project. So I will show you guys how to make a simple project by making a small currency converter. And this is the first part of the tutorial. This article consists information about:

  • YQL console which is pretty cool.
  •  A few things about android activities.

Lets begin!
YQL

If we want to mention about YQL console, it's a huge structure, a remote database which is managed by Yahoo. We use A modified version of SQL which is called YQL. And we Query the Yahoo's databases and many other websites.There are tons of open databases. And it's pretty useful.

Okay, we dont really need to sign up in order to use those databases. They're mostly open. Basically, we make a request from yql database. and the response will consist the data we need. All those request and responses can be in XML of JSON language. And we will parse before showing.

Go to http://developer.yahoo.com/yql/console -> And click on "Show Community tables"
For exchange, we can use yahoo.finance.xchance database. Whenever we click on the name of database, a test statement and test data will take place. You must see something like this:



We can parse XML or JSON. I've decided to use JSON because it gives us more practical response. Click on the JSON button and disable the "diagnostics". You will see a link at the bottom something starts like http://query.yahooapis.com/v1.. Copy the link.

Look at the link, do you see that if we replace the "EURUSD" with something else, we will get another response. So we will to split this link into 3 parts.



  1. private String requestWord1 =http://query.yahooapis.com/v1/public/yql</code><code>=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22";
  2. private String requestword2 = "EURUSD";
  3. private String requestWord3=%22)&amp;format=json&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&amp;callback=";


Now we only need to change the "requestword2" before making a request. Then combine these 3 strings and make a request by a HTTPRequest class.

Manifest file of the project:

Permissions:Firstly, we need to set our manifest file. I guess we only need Internet. So our permission statement is:

&lt; uses-permission android:name ="android.permission.INTERNET" /&gt;

And you had better if you change the minimum required sdk from 8 to 11. Android manifest file is done for now. Let's move to the activity design :)

And our manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apps.currency"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.apps.currency.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity:
There are thousands of different ways to order elements in your activity. You can set the dimensions as


  • "wrap_content",
  • "match_parent",
  • "Fill_parent"


I've decided to set the width or height as "wrap_content" It's a better approach if we use "dp" or "pix" while setting width or height.

activity_main.xml
android:id="@+id/entrybox"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:gravity="center"
android:inputType="number"
/>
<EditText
android:id="@+id/resultbox"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_below="@id/entrybox"
android:layout_toLeftOf="@+id/spinnerentry"
android:ems="10"
android:gravity="center"
android:inputType="number"
/>
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/currencyCode"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:visibility="invisible" />
The graphical output of this xml:



Right now our dropdowns(spinners) are all empty. And there's nothing inside of our editText elements. So far we've only set the appearance. We will handle the rest of them in the following tutorial.

Simple? It seems easier :)
On the next tutorials, we will modify the java methods to add more and more capabilities. This is the end of this tutorial. Thanks for reading.

And here you can download the whole project.
currency.zip

Hiç yorum yok:

Yorum Gönder