0% found this document useful (0 votes)
19 views3 pages

Madp 4

The document contains XML and Java code for a currency converter Android app. The XML defines the user interface layout with edit texts for input/output amounts, spinners to select currencies, and a convert button. The Java code handles setting up the UI, currency conversion rates map, and button click to convert between currencies.

Uploaded by

coloringcraft318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Madp 4

The document contains XML and Java code for a currency converter Android app. The XML defines the user interface layout with edit texts for input/output amounts, spinners to select currencies, and a convert button. The Java code handles setting up the UI, currency conversion rates map, and button click to convert between currencies.

Uploaded by

coloringcraft318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

XML:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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"
android:orientation="vertical"
android:gravity="center_vertical"
tools:context=".MainActivity">

<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:layout_marginLeft="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fromedit"
android:layout_weight="1"
android:inputType="number"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:id="@+id/fromspinner"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<EditText
android:id="@+id/toedit"
android:layout_marginLeft="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />

<Spinner
android:id="@+id/tospinner"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<Button
android:id="@+id/convertbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="15dp"
android:text="Convert"/>
</LinearLayout>

JAVA:
package com.example.currenctconverter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

String fromCurrency = "",toCurrency="",finalConvert="";

HashMap<String,Float> map = new HashMap<>();

Button convert;
EditText fromet,toet;
Spinner fromspiner,tospiner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String [] adapterArray = {"Select Currency","USD","INR","EURO","YUAN"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,adapterArray);

adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_
dropdown_item);

map.put("USDTOINR",80f);
map.put("USDTOEURO",0.92f);
map.put("USDTOYUAN",7.19f);

map.put("INRTOUSD",0.012f);
map.put("INRTOEURO",0.011f);
map.put("INRTOYUAN",0.087f);

map.put("EUROTOUSD",1.09f);
map.put("EUROTOINR",90.13f);
map.put("EUROTOYUAN",7.81f);

map.put("YUANTOUSD",0.14f);
map.put("YUANTOINR",11.77f);
map.put("YUANTOEURO",0.13f);

convert = (Button) findViewById(R.id.convertbtn);


fromet = (EditText) findViewById(R.id.fromedit);
toet = (EditText) findViewById(R.id.toedit);

fromspiner = (Spinner) findViewById(R.id.fromspinner);


tospiner = (Spinner) findViewById(R.id.tospinner);

fromspiner.setAdapter(adapter);
tospiner.setAdapter(adapter);
fromspiner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
fromCurrency=fromspiner.getSelectedItem().toString();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

tospiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
toCurrency = tospiner.getSelectedItem().toString();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!fromCurrency.equals(adapterArray[0])&&!
toCurrency.equals(adapterArray[0])) {
finalConvert = fromCurrency + "TO" + toCurrency;
float currency = map.get(finalConvert);
float res = Float.parseFloat(fromet.getText().toString()) *
currency;
toet.setText(Float.toString(res));
} else{
Toast.makeText(getApplicationContext(),"Select
Currency",Toast.LENGTH_LONG).show();
}
}
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy