package com.example.nabeel.myapplicationtutorial2;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class CoolCalc extends Activity implements View.OnClickListener {
Button zeroBtn, oneBtn, twoBtn, threeBtn, fourBtn, fiveBtn, sixBtn, sevenBtn, eightBtn, nineBtn, clearBtn;
ImageButton addBtn, subtractBtn, multiplyBtn, divideBtn,calcBtn;
TextView resultView;
String runningNumber = "";
String leftValueStr = "";
String rightValueStr = "";
Operation currentOperation;
int result = 0;
public enum Operation{
ADD, SUBTRACT, MULLTIPLY, DIVIDE, EQUAL;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cool_calc);
init();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.zeroBtn:
numberPressed(0);
break;
case R.id.oneBtn:
numberPressed(1);
break;
case R.id.twoBtn:
numberPressed(2);
break;
case R.id.threeBtn:
numberPressed(3);
break;
case R.id.fourBtn:
numberPressed(4);
break;
case R.id.fiveBtn:
numberPressed(5);
break;
case R.id.sixBtn:
numberPressed(6);
break;
case R.id.sevenBtn:
numberPressed(7);
break;
case R.id.eightBtn:
numberPressed(8);
break;
case R.id.nineBtn:
numberPressed(9);
break;
case R.id.addBtn:
processOperation(Operation.ADD);
break;
case R.id.subtractBtn:
processOperation(Operation.SUBTRACT);
break;
case R.id.multiplyBtn:
processOperation(Operation.MULLTIPLY);
break;
case R.id.divideBtn:
processOperation(Operation.DIVIDE);
break;
case R.id.clearBtn:
leftValueStr = "";
rightValueStr= "";
currentOperation = null;
runningNumber = "";
result = 0;
resultView.setText("0");
break;
case R.id.calcBtn:
processOperation(Operation.EQUAL);
break;
}
}
private void init() {
//inialize all xml attributes
zeroBtn = findViewById(R.id.zeroBtn);
oneBtn = findViewById(R.id.oneBtn);
twoBtn = findViewById(R.id.twoBtn);
threeBtn = findViewById(R.id.threeBtn);
fourBtn = findViewById(R.id.fourBtn);
fiveBtn = findViewById(R.id.fiveBtn);
sixBtn = findViewById(R.id.sixBtn);
sevenBtn = findViewById(R.id.sevenBtn);
eightBtn = findViewById(R.id.eightBtn);
nineBtn = findViewById(R.id.nineBtn);
clearBtn = findViewById(R.id.clearBtn);
resultView = findViewById(R.id.resultView);
addBtn = findViewById(R.id.addBtn);
subtractBtn = findViewById(R.id.subtractBtn);
multiplyBtn = findViewById(R.id.multiplyBtn);
divideBtn = findViewById(R.id.divideBtn);
calcBtn = findViewById(R.id.calcBtn);
//set listeners
zeroBtn.setOnClickListener(this);
oneBtn.setOnClickListener(this);
twoBtn.setOnClickListener(this);
threeBtn.setOnClickListener(this);
fourBtn.setOnClickListener(this);
fiveBtn.setOnClickListener(this);
sixBtn.setOnClickListener(this);
sevenBtn.setOnClickListener(this);
eightBtn.setOnClickListener(this);
nineBtn.setOnClickListener(this);
resultView.setOnClickListener(this);
addBtn.setOnClickListener(this);
subtractBtn.setOnClickListener(this);
multiplyBtn.setOnClickListener(this);
divideBtn.setOnClickListener(this);
calcBtn.setOnClickListener(this);
clearBtn.setOnClickListener(this);
}
void numberPressed( int number ){
runningNumber +=String.valueOf(number);
resultView.setText(runningNumber);
}
void processOperation(Operation operation){
if (currentOperation != null){
if (runningNumber != null){
rightValueStr = runningNumber;
runningNumber = "";
switch (currentOperation){
case ADD:
result = Integer.parseInt(leftValueStr) + Integer.parseInt(rightValueStr);
break;
case SUBTRACT:
result = Integer.parseInt(leftValueStr) - Integer.parseInt(rightValueStr);
break;
case MULLTIPLY:
result = Integer.parseInt(leftValueStr) * Integer.parseInt(rightValueStr);
break;
case DIVIDE:
result = Integer.parseInt(leftValueStr) / Integer.parseInt(rightValueStr);
break;
}
leftValueStr = String.valueOf(result);
resultView.setText(leftValueStr);
}
}
else {
leftValueStr = runningNumber;
runningNumber = "";
}
currentOperation = operation;
}
}
<?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"
tools:context=".CoolCalc"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="165dp"
android:id="@+id/result_text"
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultView"
android:layout_gravity="end"
android:gravity="right"
android:textColor="@android:color/black"
android:text=""
android:paddingTop="50dp"
android:textSize="45sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/sevenBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="7"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/eightBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="8"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/nineBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="9"
android:textColor="#000000"
android:textSize="30dp" />
<ImageButton
android:id="@+id/divideBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:padding="16dp"
android:src="@drawable/icon_divide" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="@+id/fourBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="4"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/fiveBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="5"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/sixBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="6"
android:textColor="#000000"
android:textSize="30dp" />
<ImageButton
android:id="@+id/multiplyBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:padding="16dp"
android:src="@drawable/icon_multiplication" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="@+id/oneBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="1"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/twoBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="2"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/threeBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="3"
android:textColor="#000000"
android:textSize="30dp" />
<ImageButton
android:id="@+id/subtractBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:padding="16dp"
android:src="@drawable/icon_minus" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="?android:attr/colorForeground"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="110dp"
android:layout_height="wrap_content"
android:id="@+id/clearBtn"
android:background="#45c4cd"
android:text="CLEAR"
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
<Button
android:id="@+id/zeroBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/colorForeground"
android:text="0"
android:textColor="#000000"
android:textSize="30dp" />
<ImageButton
android:id="@+id/addBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:padding="16dp"
android:src="@drawable/add" />
</LinearLayout>
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/calcBtn"
android:layout_alignParentEnd="true"
android:layout_marginTop="130dp"
android:layout_marginEnd="50dp"
android:background="@android:color/white"
android:contentDescription="@null"
android:src="@drawable/equal"
tools:ignore="RtlSymmetry" />
</RelativeLayout>