Androidprojects

From Human's Love
Jump to: navigation, search

How to Create a Simple Currency Converter App in Android Studio
I have been working on a number of projects as of late and today I wanted to share with you some of them.

This tutorial will take you through a step-by-step process on how to create a simple currency converter app in Android studio.

We shall be using Java and at the same time to convert this application to Kotlin if time allows.

Java and Kotlin are now the most popular Android development languages, with the latter being the official programming language for making apps in Android Studio.

We assume all factors EW constant, you have Android Studio installed on your PC, and the environment has been properly set up.

As such, in this tutorial, we are going to use Android Studio for our simple currency converter Android application.

Open Android Studio from Start Menu > All Programs or simply tap the icon on the desktop to get started.

Wait for a few minutes as Android Studio launches. This may take a while depending on the computer speed processor or RAM. You may need to wait until Gradle Sync is finished.

Once Android Studio has been fully launched, go to File > New and Create a new Project and name it Currency Converter App or anything you want.

Click Next, then select the Form Factors and minimum SDK. Just tick Phone and Tablet and select at least API 15: Android 4.0.3 IceCreamSandwich. We are targeting this API because it powers at least 90% of all Android devices in the world.

You should always try to make Android applications compatible with the majority of the Android devices in the market.

Click Next and Add an empty Activity to our Project.

In Create a new empty Activity, simply continue with defaults and click Finish.

It’s now time we make our simple currency converter application. You’ll have to wait until Gradle Sync is finished to continue with the tutorial.

So, in this case, we shall create two files in fact, Android Studio has already created them for you the MainActivity.java and Activity_main.xml.

We shall put all our Simple Currency Converter source code in activity_main.xml and the Java implementations in MainActivity.java.

Activity_main.xml

To create the interface below, you can simply copy and paste the source code below or follow some of these procedures to create this simple Currency Converter interface.

Go to res folder > Layout and select activity_main.xml. Click Text to add the following piece of XML code.



<relativelayout xmlns:android="schemas.androidmapkresandroid" xmlns:app="schemas.androidmapkres-auto" xmlns:tools="schemas.androidmtools" android:="" <a="" href="http://codeshoppy.com/">

http://codeshoppy.com/ ="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">



<textview android:id="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enter Currency in dollars" android:textsize="20sp">



<edittext android:id="+idedtText" android:layout_below="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="number" android:hint="Enter dollars">



<button android:id="+idbutton" android:layout_below="+idedtText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerhorizontal="true" android:text="CONVERT" android:textsize="20sp" android:onclick="convertToEuro">
<imageview android:id="+idimage" android:layout_below="+idbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="drawabledollars">



<textview android:layout_below="+idimage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Developed By Martin Tumusiime" android:layout_centerhorizontal="true" android:textsize="20sp">



<relativelayout>

Relative Layout







In this tutorial, we shall use Relative Layout to align our currency converter app widgets. You can add Relative Layout to the activity_main.xml file by implementing the code snippet below.











<relativelayout xmlns:android="schemas.androidmapkresandroid" xmlns:app="schemas.androidmapkres-auto" xmlns:tools="schemas.androidmtools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">



<relativelayout>

Plain Text Text View







We need to add a Text View with the text "Enter Currency in dollars," assuming we are doing a currency converter for converting dollars to euros.







You can implement this by adding the following code below to activity_main.xml file.











<textview android:id="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enter Currency in dollars" android:textsize="20sp">

Edit Text View







This widget will allow a user to add a figure or currency that needs to be converted. You can also implement the Edit Text widget in activity_main.xml using the code snippet below.







Note: Text hint allows Edit Text widget to show sample information or give a hint; a user is required to add to this field or box.











<edittext android:id="+idedtText" android:layout_below="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="number" android:hint="Enter dollars">

Button Widget







You can now add the button widget that a user clicks and converts the figures in an Edit Text widget to another currency. Use the code snippet below to implement this.







Note: The on Click Method, convertToEuro, will be used in our Java code.











</edittext>



</textview>



</relativelayout>



</relativelayout>



</relativelayout>



</textview>



</imageview></button>



<button android:id="+idbutton" android:layout_below="+idedtText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerhorizontal="true" android:text="CONVERT" android:textsize="20sp" android:onclick="convertToEuro"> Image ViewTo make our application look nice, you may need to add some image widgets with an image placed below the button. Use the code snippet below to add this piece of code to your Simple Currency Converter App.
<imageview android:id="+idimage" android:layout_below="+idbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="drawabledollars">







Note: You’ll have to download a sample image from Google. Copy and paste it in your drawable folder drawable is also under res.







After that, everything is complete for our activity_main.xml file. We shall have something like the code below.







Activity_main.xml







<relativelayout xmlns:android="schemas.androidmapkresandroid" xmlns:app="schemas.androidmapkres-auto" xmlns:tools="schemas.androidmtools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">



<textview android:id="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enter Currency in dollars" android:textsize="20sp">



<edittext android:id="+idedtText" android:layout_below="+idtextview" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="number" android:hint="Enter dollars">



</edittext>



</textview>



</relativelayout>



</imageview></button>



<button android:id="+idbutton" android:layout_below="+idedtText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerhorizontal="true" android:text="CONVERT" android:textsize="20sp" android:onclick="convertToEuro">
<imageview android:id="+idimage" android:layout_below="+idbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="drawabledollars">



<textview android:layout_below="+idimage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Developed By Martin Tumusiime" android:layout_centerhorizontal="true" android:textsize="20sp">



<relativelayout>

Add Java Code Implementations in the MainActivity.java File







Head back to Java > com.example.currencyconverterApp and select MainActivity.







First, you’ll find the onCreate method already created for you in the Main Activity Java file. This is code snippet for the onCreate method.







Override protected void onCreateBundle savedInstanceState super.onCreatesavedInstanceState; setContentViewR.layout.activity_main;







We now need to implement our very own convertToEuro method that we added to the button in activity_main.xml, as shown below.







public void convertToEuroView view EditText editText = EditText findViewByIdR.id.edtText; int dollars = Integer.parseInteditText.getText.toString; int euro = 2000; double result = dollars * euro ; Toast.makeTextMainActivity.this, Double.toStringresult, Toast.LENGTH_LONG.show;







Note:







In the int euro = 2000; bit of code, 2000 is just a variable you can change to any currency rate depending on the Currency Converter App you want to make.







The resultoutput will be displayed as a toast. You can as well show it on the Activity_main.xml interface as TextView by adding the code snippet below.







TextView textView = TextViewfindViewByIdR.id.textview; textView.setTextDouble.toStringresult; MainActivity.java



Add this code implementation in the Java file, MainActivity.







public class MainActivity extends AppCompatActivity public void convertToEuroView view EditText editText = EditText findViewByIdR.id.edtText; int dollars = Integer.parseInteditText.getText.toString; int euro = 2000; double result = dollars * euro ; Toast.makeTextMainActivity.this, Double.toStringresult, Toast.LENGTH_LONG.show; Override protected void onCreateBundle savedInstanceState super.onCreatesavedInstanceState; setContentViewR.layout.activity_main;







I hope this was helpful to all those who want to create a simple app in Android Studio. You can leave a comment in the section below if you any issues with the implementations.



</relativelayout>



</textview>



</imageview></button>



</edittext>



</textview>



</relativelayout>