Android конвертер валют java

How to Create a Simple Currency Converter App in Android Studio

Join the DZone community and get the full member experience.

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.

Читайте также:  How to replace all in javascript

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.

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

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.

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.

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.

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.

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.

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

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)

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

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

Opinions expressed by DZone contributors are their own.

Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake

Источник

Create real time currency converter app in Android Studio

In this tutorial, we will learn to create a Currency converter app in android using java. We can develop an app in android either using java or kotlin. To develop an android project we can either use Eclipse IDE or we can directly use Android Studio (any version). We will use: exchangeratesapi.io API key here.

For Eclipse IDE we need to download and install Android SDK and ADT plugin and Android version which is a very lengthy process. So instead of Eclipse IDE, the Android studio is preferred by most of the developers.

In this tutorial, we will use Android Studio 3.6.3 (latest version).

To create a new project in android studio: currency converter

  1. Click on the file option on the upper right corner of our android studio screen.
  2. After that, we can select a project template.

Android Studio Screen

3. On clicking on the next option we will get the screen to configure the project. Here we can select the name of our project, package name, location to store our project, language, and minimum SDK.

4. Click on the finish button.

Create project in android studio

Start coding

In the above figure, there are two options

In MainActivity.java we will write our java code and activity_main.xml we will design the view of our app.

To design the view of our app we will get two option

  1. We can directly drag and drop the required tool to our emulator.
  2. Write an XML code to design the view of our application.

Code:

package com.example.myapplication; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.JsonObject; import Kamal.kamalBuild; import Kamal.kamalInterface; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends AppCompatActivity < Button button; EditText currencyToBeConverted; EditText currencyConverted; Spinner convertToDropdown; Spinner convertFromDropdown; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); currencyConverted = (EditText) findViewById(R.id.currency_converted); currencyToBeConverted = (EditText) findViewById(R.id.currency_to_be_converted); convertToDropdown = (Spinner) findViewById(R.id.to); convertFromDropdown = (Spinner) findViewById(R.id.from); button = (Button) findViewById(R.id.button); String[] dropDownList = ; ArrayAdapter adapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, dropDownList); convertToDropdown.setAdapter(adapter); convertFromDropdown.setAdapter(adapter); button.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < kamalInterface retrofitInterface = kamalBuild.getRetrofitInstance().create(kamalInterface.class); Callcall = retrofitInterface.getExchangeCurrency(convertFromDropdown.getSelectedItem().toString()); call.enqueue(new Callback() < @Override public void onResponse(Callcall, Response response) < JsonObject res = response.body(); JsonObject rates = res.getAsJsonObject("conversion_rates"); double currency = Double.valueOf(currencyToBeConverted.getText().toString()); double multiplier = Double.valueOf(rates.get(convertToDropdown.getSelectedItem().toString()).toString()); double result = currency * multiplier; currencyConverted.setText(String.valueOf(result)); >@Override public void onFailure(Call call, Throwable t) < >>); > >);>> ----------------------------------------------------------- create a package and in that package create one interface and one class. ----------------------------------------------------------- import retrofit2.converter.gson.GsonConverterFactory; public class kamalBuild < private static Retrofit retrofit; public static Retrofit getRetrofitInstance() < if (retrofit == null) < retrofit = new Retrofit.Builder() .baseUrl("https://api.exchangerate-api.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); >return retrofit; > > ----------------------------------------------------------- package Kamal; import com.google.gson.JsonObject; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; public interface kamalInterface < @GET("v6/1929988978e7cd9733e5e654/latest/") Call getExchangeCurrency(@Path("currency") String currency); > -----------------------------------------------------------

MainActivity.java

The name of our package is com.example.myapplication.

We generally use Bundle to pass the data between various activities of Android. We can map the data from the string value to various data types with the help of parsing using Bundle.

The view is the rectangular piece of the area at the front end of our application.

Array Adapter converts the Array List of objects into view items that can be loaded into a listview container.

AppCompat is the set of libraries that are used to develop the app with various versions. This means when a new version of the android studio gets released the appcompat helps us to work with old libraries from the new version.

Gson is called google JSON and JSON stands for javascript object notation. It is used to convert data to JSON or XML code. This means when we pass any data to gson it takes the java object and returns a corresponding JSON.

Retrofit is the HTTP client it helps to retrieve and upload JSON via web services.

Oncreate()

In the onCreate method of our project, we created objects of TextView, EditText, spinner, and button.

Then we created a drop-down list in which we passed the currencies of several countries.

Then in the onClick method of our project, we are converting the values from the currency of one country to the currency of any other country depending on the choice of the user. We are using the package that we created to implement the libraries and function of google JSON and Retrofit to convert our currency. We just passing the values and with the help of functionalities of retrofit and GSON that we created in the package, we are converting the currency.

The res.getAsJsonObject(“conversion_rates”) method helps us to get the rates to which we need to multiply the currency to get the required currency.

Further SetText is used to display the result obtained by multiplying the currency entered and the rate generated on the front end of the app.

KamalInterface

Retrofit makes downloading XML or JSON data from web API easier.

In the package kamal, we are creating an instance of retrofit in retrofitInterface.

We are using Exchange Rates-API

In the retrofit interface, 1929988978e7cd9733e5e654 is the key to our API used.V6 is the version of the API.

Just enter your email id to get the API key.

Method getExchangeCurrency(@Path(“currency”) String currency) is used to get the rate of exchange of the currency.

In the above method, the “currency” is used because we are selecting the currency from the drop-down menu. Instead of currency, we can also write “USD”, “EUR”, etc.

KamalBuild

In kamalBuild, we are creating the instance of Retrofit in which we are passing the base address of our API and addConvertFactory method.

To run our application we need to add these dependencies in the Gradle file of our project.

These dependencies are for version 29.

XML Code

We can modify the front end of our app according to our needs. We can add images, background colors, etc.

To add an image we need to download an image and save it in the drawable folder. From there we can add it to our ImageView from the src option.

We can even add audio to our app. For adding any audio file we need to create a raw folder in our app and then we can add the audio file to our app.

In the above XML code, first of all, we set Layout to LinearLayout.

ImageView helps us to display images.

EdiText helps us to take input from the user.

The button helps us to perform any operation with the help of any method mentioned in the onClick function.

Spinner helps us to display a drop-down menu.

Spinner is used for creating a drop-down menu. We can modify our spinner, Button, EditText TextView, etc in our XML code.

Modifying spinner, Button, EditText, TextView, etc means adding curves, changing color, and much more to give an impressive and attractive look to the app.

In the above XML code, we are just creating id of our image views, button, spinner, text view, and edit view. And adjusting them according to the view we want.

Источник

Android конвертер валют java

We made the layout design in activity_main.xml

activity_main.xml

MainActivity

package com.example.kobinath.currency; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity < Spinner sp1,sp2; EditText ed1; Button b1; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp1 = findViewById(R.id.spfrom); sp2 = findViewById(R.id.spto); ed1 = findViewById(R.id.txtamt); String[] from = ; ArrayAdapter ad = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,from); sp1.setAdapter(ad); String[] to = ; ArrayAdapter ad1 = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,to); sp2.setAdapter(ad1); b1 = findViewById(R.id.btn1); b1.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < Double tot; Double amt = Double.parseDouble(ed1.getText().toString()); if(sp1.getSelectedItem().toString() == "USD" && sp2.getSelectedItem().toString() == "Indian Rupees") < tot = amt * 70.0; Toast.makeText(getApplicationContext(),tot.toString(),Toast.LENGTH_LONG).show(); >else if(sp1.getSelectedItem().toString() == "USD" && sp2.getSelectedItem().toString() == "Srilankan Rupees") < tot = amt * 179.0; Toast.makeText(getApplicationContext(),tot.toString(),Toast.LENGTH_LONG).show(); >> >); > >

I have attached the video tutorial below it will help you to do this step by step.

Источник

Оцените статью