In this tutorial, I will show you how to create simple calculator in Android. See output below!
Here's the CalculatorModel.java
package com.example.calculator;
public class CalculatorModel {
double m_num1, m_num2;
//constructor
public CalculatorModel(double num1, double num2){
this.m_num1 = num1;
this.m_num2 = num2;
}
//setters
public void setNum1(double num1){
this.m_num1 = num1;
}
public void setNum2(double num2){
this.m_num2 = num2;
}
//getters
public double getNum1(){
return this.m_num1;
}
public double getNum2(){
return this.m_num2;
}
}
Here's the CalculatorController.java
package com.example.calculator;
public class CalculatorController {
public double addition(CalculatorModel cmodel){
return cmodel.m_num1 + cmodel.m_num2;
}
public double subtraction(CalculatorModel cmodel){
return cmodel.m_num1 - cmodel.m_num2;
}
public double multiplication(CalculatorModel cmodel){
return cmodel.m_num1 * cmodel.m_num2;
}
public double divistion(CalculatorModel cmodel){
return cmodel.m_num1 / cmodel.m_num2;
}
}
Here's the CalculatorActivity.java
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CalculatorActivity extends Activity implements OnClickListener{
EditText et_result;
Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven,
btn_eight, btn_nine, btn_zero, btn_point, btn_add, btn_minus, btn_times,
btn_divide, btn_equal, btn_clear;
double num1 = 0;
int indicator = 0;
CalculatorModel c_model;
CalculatorController c_controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
initializeVars();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void initializeVars(){
et_result = (EditText)findViewById(R.id.et_result);
btn_one = (Button)findViewById(R.id.btn_one);
btn_two = (Button)findViewById(R.id.btn_two);
btn_three = (Button)findViewById(R.id.btn_three);
btn_four = (Button)findViewById(R.id.btn_four);
btn_five = (Button)findViewById(R.id.btn_five);
btn_six = (Button)findViewById(R.id.btn_six);
btn_seven = (Button)findViewById(R.id.btn_seven);
btn_eight = (Button)findViewById(R.id.btn_eight);
btn_nine = (Button)findViewById(R.id.btn_nine);
btn_zero = (Button)findViewById(R.id.btn_zero);
btn_point = (Button)findViewById(R.id.btn_point);
btn_add = (Button)findViewById(R.id.btn_add);
btn_minus = (Button)findViewById(R.id.btn_minus);
btn_times = (Button)findViewById(R.id.btn_times);
btn_divide = (Button)findViewById(R.id.btn_divide);
btn_equal = (Button)findViewById(R.id.btn_equal);
btn_clear = (Button)findViewById(R.id.btn_clear);
btn_one.setOnClickListener(this);
btn_two.setOnClickListener(this);
btn_three.setOnClickListener(this);
btn_four.setOnClickListener(this);
btn_five.setOnClickListener(this);
btn_six.setOnClickListener(this);
btn_seven.setOnClickListener(this);
btn_eight.setOnClickListener(this);
btn_nine.setOnClickListener(this);
btn_zero.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_times.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
switch(v.getId()){
case R.id.btn_one:
et_result.setText(et_result.getText().toString()+btn_one.getText().toString());
break;
case R.id.btn_two:
et_result.setText(et_result.getText().toString()+btn_two.getText().toString());
break;
case R.id.btn_three:
et_result.setText(et_result.getText().toString()+btn_three.getText().toString());
break;
case R.id.btn_four:
et_result.setText(et_result.getText().toString()+btn_four.getText().toString());
break;
case R.id.btn_five:
et_result.setText(et_result.getText().toString()+btn_five.getText().toString());
break;
case R.id.btn_six:
et_result.setText(et_result.getText().toString()+btn_six.getText().toString());
break;
case R.id.btn_seven:
et_result.setText(et_result.getText().toString()+btn_seven.getText().toString());
break;
case R.id.btn_eight:
et_result.setText(et_result.getText().toString()+btn_eight.getText().toString());
break;
case R.id.btn_nine:
et_result.setText(et_result.getText().toString()+btn_nine.getText().toString());
break;
case R.id.btn_zero:
et_result.setText(et_result.getText().toString()+btn_zero.getText().toString());
break;
case R.id.btn_point:
et_result.setText(et_result.getText().toString()+btn_point.getText().toString());
break;
case R.id.btn_add:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 1;
break;
case R.id.btn_minus:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 2;
break;
case R.id.btn_times:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 3;
break;
case R.id.btn_divide:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 4;
break;
case R.id.btn_clear:
et_result.setText("");
break;
case R.id.btn_equal:
c_model = new CalculatorModel(num1, Double.parseDouble(et_result.getText().toString()));
c_controller = new CalculatorController();
switch(indicator){
case 1:
et_result.setText(Double.toString(c_controller.addition(c_model)));
break;
case 2:
et_result.setText(Double.toString(c_controller.subtraction(c_model)));
break;
case 3:
et_result.setText(Double.toString(c_controller.multiplication(c_model)));
break;
case 4:
et_result.setText(Double.toString(c_controller.divistion(c_model)));
break;
}
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
And lastly, the activity_calculator.xml
<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=".CalculatorActivity" >
<RelativeLayout
android:id="@+id/rl_hold_calculator"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
"
<EditText
android:id="@+id/et_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_result"
android:layout_below="@+id/et_result"
android:layout_marginTop="10dp"
android:text="1" />
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_one"
android:layout_alignBottom="@+id/btn_one"
android:layout_toRightOf="@+id/btn_one"
android:text="2" />
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_two"
android:layout_alignBottom="@+id/btn_two"
android:layout_toRightOf="@+id/btn_two"
android:text="3" />
<Button
android:id="@+id/btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_three"
android:layout_alignBottom="@+id/btn_three"
android:layout_toRightOf="@+id/btn_three"
android:text="4" />
<Button
android:id="@+id/btn_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_one"
android:layout_below="@+id/btn_one"
android:layout_marginTop="10dp"
android:text="5" />
<Button
android:id="@+id/btn_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_five"
android:layout_alignBottom="@+id/btn_five"
android:layout_alignLeft="@+id/btn_two"
android:text="6" />
<Button
android:id="@+id/btn_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_six"
android:layout_alignBottom="@+id/btn_six"
android:layout_alignLeft="@+id/btn_three"
android:text="7" />
<Button
android:id="@+id/btn_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_seven"
android:layout_alignBottom="@+id/btn_seven"
android:layout_toRightOf="@+id/btn_seven"
android:text="8" />
<Button
android:id="@+id/btn_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_five"
android:layout_below="@+id/btn_five"
android:layout_marginTop="10dp"
android:text="9" />
<Button
android:id="@+id/btn_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_nine"
android:layout_alignBottom="@+id/btn_nine"
android:layout_toRightOf="@+id/btn_nine"
android:text="0" />
<Button
android:id="@+id/btn_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_zero"
android:layout_alignBottom="@+id/btn_zero"
android:layout_toRightOf="@+id/btn_zero"
android:text="." />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_point"
android:layout_alignBottom="@+id/btn_point"
android:layout_toRightOf="@+id/btn_point"
android:text="C" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_nine"
android:layout_below="@+id/btn_nine"
android:layout_marginTop="10dp"
android:text="+" />
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_add"
android:layout_alignBottom="@+id/btn_add"
android:layout_toRightOf="@+id/btn_add"
android:text="-" />
<Button
android:id="@+id/btn_times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_minus"
android:layout_alignBottom="@+id/btn_minus"
android:layout_alignLeft="@+id/btn_point"
android:text="*" />
<Button
android:id="@+id/btn_divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_times"
android:layout_alignBottom="@+id/btn_times"
android:layout_alignLeft="@+id/btn_clear"
android:text="/" />
<Button
android:id="@+id/btn_equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_add"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_add"
android:layout_marginTop="10dp"
android:text="=" />
</RelativeLayout>
</RelativeLayout>