DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Build a React Native Chat App for Android
  • How it Feels to Switch from Eclipse to Android Studio
  • The 12 Biggest Android App Development Trends in 2023
  • Android Cloud Apps with Azure

Trending

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • Agentic AI Systems: Smarter Automation With LangChain and LangGraph
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Create a Simple Currency Converter App in Android Studio

How to Create a Simple Currency Converter App in Android Studio

Learn to create your own currency converter as a means of learning the core concepts behind Android application development in Java.

By 
Martin Tumusiime user avatar
Martin Tumusiime
·
Updated Feb. 12, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
65.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Simple Currency Converter Android App Example and How to Create It

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

open android studio

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.

Create new simple currency converter app

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.

add an empty activity

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.

<?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">
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Currency in dollars"
        android:textSize="20sp"/>
    <EditText
        android:id="@+id/edtText"
        android:layout_below="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="Enter dollars"/>
    <Button
        android:id="@+id/button"
        android:layout_below="@+id/edtText"
        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="@+id/image"
        android:layout_below="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dollars"/>
    <TextView
        android:layout_below="@+id/image"
        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="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">
</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="@+id/textview"
        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="@+id/edtText"
        android:layout_below="@+id/textview"
        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.

<Button
        android:id="@+id/button"
        android:layout_below="@+id/edtText"
        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 View

To 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="@+id/image"
        android:layout_below="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dollars"/>

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

<?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">
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Currency in dollars"
        android:textSize="20sp"/>
    <EditText
        android:id="@+id/edtText"
        android:layout_below="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="Enter dollars"/>
    <Button
        android:id="@+id/button"
        android:layout_below="@+id/edtText"
        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="@+id/image"
        android:layout_below="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dollars"/>
    <TextView
        android:layout_below="@+id/image"
        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 onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.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 convertToEuro(View view){
    EditText editText = (EditText) findViewById(R.id.edtText);

    int dollars = Integer.parseInt(editText.getText().toString());
    int euro = 2000;

    double result = dollars * euro ;

    Toast.makeText(MainActivity.this, Double.toString(result), Toast.LENGTH_LONG).show();
}

Note:

  1. 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.

  2. The result/output 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 = (TextView)findViewById(R.id.textview);
textView.setText(Double.toString(result));

MainActivity.java

Add this code implementation in the Java file, MainActivity.

public class MainActivity extends AppCompatActivity {

    public void convertToEuro(View view){
        EditText editText = (EditText) findViewById(R.id.edtText);

        int dollars = Integer.parseInt(editText.getText().toString());
        int euro = 2000;

        double result = dollars * euro ;

        Toast.makeText(MainActivity.this, Double.toString(result), Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.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.

Android Studio Android (robot) app code style

Published at DZone with permission of Martin Tumusiime. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Build a React Native Chat App for Android
  • How it Feels to Switch from Eclipse to Android Studio
  • The 12 Biggest Android App Development Trends in 2023
  • Android Cloud Apps with Azure

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!