Madp 4
Madp 4
<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;
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);
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();
}
}
});
}
}