Simple Calculator with Spinner – Android Code

This code is a simple code for Android application. It has two textboxes to receive two numbers (decimal), list of mathematical operations with +, –, * and /. Choose the operation, and hit the “Display result” button. You will see the answer to the at the bottom of the button. Have fun with Android.

image image

The Manifest file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText android:text="@+id/EditText01" 
	android:id="@+id/EditText01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
	</EditText>
<EditText android:text="@+id/EditText02" 
	android:id="@+id/EditText02" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
	</EditText>
<Spinner android:id="@+id/Spinner01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
	</Spinner>
	
<Button android:text="@+id/Button01" 
	android:id="@+id/Button01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
	</Button>
<TextView android:text="@+id/TextView01" 
	android:id="@+id/TextView01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
	</TextView>
</LinearLayout>


The Code



 









package my.android.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Calc extends Activity implements OnItemSelectedListener{
    /** Called when the activity is first created. */
    EditText num1, num2;
	Button button01;
	TextView tv1;
	ImageView iv1;
	Spinner spinOps;
	String[] ops={"Choose operation","+","-","*","/"};
	String opselected;
	double a, b, c;
    private OnClickListener myClickListener = new OnClickListener()
    {
    	public void onClick(View v) {
    		a=Double.parseDouble(num1.getText().toString()); 
    		b=Double.parseDouble(num2.getText().toString()); 
    		if (opselected=="+"){c=a+b; tv1.setText(Double.toString(c));}
    		else if (opselected=="-"){c=a-b; tv1.setText(Double.toString(c));}
    		else if (opselected=="*"){c=a*b; tv1.setText(Double.toString(c));}
    		else if (opselected=="/"){c=a/b; tv1.setText(Double.toString(c));}
    		else {tv1.setText("Please select the operation");}
    		//tv1 = (TextView)findViewById(R.id.TextView01);
    		
    		
    	}
    };
        @Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
            
           tv1 = (TextView)findViewById(R.id.TextView01);
           //iv1= (ImageView)findViewById(R.id.);//try imageview
           
           button01 = (Button)findViewById(R.id.Button01);
           button01.setText("Display result");
           button01.setOnClickListener(myClickListener);
           
           num1 = (EditText)findViewById(R.id.EditText01);
           num1.setText("");
           num2 = (EditText)findViewById(R.id.EditText02);
           num2.setText("");
           
           //list of operations
           spinOps = (Spinner)findViewById(R.id.Spinner01);
           ArrayAdapter<String> adapter = new ArrayAdapter<String>(
             this, android.R.layout.simple_spinner_item, ops);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           spinOps.setAdapter(adapter);
           spinOps.setOnItemSelectedListener(this);
           
        }   
        public void onItemSelected (AdapterView<?> p,View v,int position,long id) {
        	opselected=ops[position];
        	tv1.setText("You have selected " + opselected);
        }
        public void onNothingSelected(AdapterView<?> p) { 
        	tv1.setText("Please select ops"); 
        }
}


image Android Developer in the Making…

Popular Posts