Enter code here...
JAVA CODE
package com.example.android.mycalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
Spinner spinner;
TextView result;
private EditText text1;
private EditText text2;
double l1,l2;
private String[] operations = {"Add","Subtract","Multiply","Divide"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
result=(TextView) findViewById(R.id.result);
spinner.setOnItemSelectedListener(this);
text1 = (EditText) findViewById(R.id.number1);
text2 = (EditText) findViewById(R.id.number2);
// l1 = Double.parseDouble(text1.getText().toString());
// l2=Double.parseDouble(text2.getText().toString());
// Spinner Drop down elements
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, operations);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public String add() {
double result=l1+l2;
return Double.toString(result);
}
public String sub() {
double result=l1-l2;
return Double.toString(result);
}
public String mul() {
double result=l1*l2;
return Double.toString(result);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
String selected = (String) spinner.getSelectedItem();
if(selected.equals(operations[0])) result.setText(add());
if(selected.equals(operations[1])) result.setText(sub());
if(selected.equals(operations[2])) result.setText(mul());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
XML CODE
Enter code here...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.mycalculator.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Operation"
android:textSize="20sp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"
android:layout_marginTop="25dp" />
<EditText
android:id="@+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="number"
android:layout_below="@+id/spinner"/>
<EditText
android:id="@+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="number"
android:layout_below="@+id/number1"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="RESULT"
android:textColor="#000000"
android:textSize="20sp"
android:layout_below="@id/number2"/>
<View
android:layout_width="150dp"
android:layout_height="2dp"
android:layout_below="@+id/result"
android:background="@android:color/darker_gray" />
</RelativeLayout>
I am trying to create a calculator app that takes two numbers and give me the
desired result on selecting the desired operation from the drop down menu
option.
My app is getting crashed and giving me the error-'Invalid double: ""'..What is
the issue with my code.I am not able to get it.
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/6bc8374d-4de6-4e46-9b65-deac3165e701%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.