Hello, Today we will make a scientific calculator for Android Using Android Studio. The Calculator will store history DB
Create A new Project name Scientific Calculator
Select Blank project.
First of all Download This Library and using your file explorer put in in your ProjecFolder/app/libs
and add this to you app build.gradle dependecies
compile files('libs/javaluator-3.0.1.jar')
like This
Type this in your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:editable="false"
android:textAlignment="textEnd"
android:gravity="end"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:editable="false"
android:textAlignment="textEnd"
android:gravity="end" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/square"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/square"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xpowy"
android:layout_alignBottom="@+id/square"
android:layout_toRightOf="@+id/square"
android:layout_toEndOf="@+id/square"
android:text="@string/xpown"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="log"
android:id="@+id/log"
android:layout_alignBottom="@+id/xpowy"
android:layout_toRightOf="@+id/xpowy"
android:layout_toEndOf="@+id/xpowy"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sin"
android:id="@+id/sin"
android:layout_below="@+id/square"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cos"
android:id="@+id/cos"
android:layout_alignBottom="@+id/sin"
android:layout_alignLeft="@+id/xpowy"
android:layout_alignStart="@+id/xpowy"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tan"
android:id="@+id/tan"
android:layout_alignBottom="@+id/cos"
android:layout_toRightOf="@+id/cos"
android:layout_toEndOf="@+id/cos"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggle"
android:text="@string/toggle"
android:layout_below="@+id/sin"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:layout_toLeftOf="@+id/cos"
android:layout_toStartOf="@+id/cos"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/factorial"
android:id="@+id/factorial"
android:layout_alignBottom="@+id/toggle"
android:layout_toRightOf="@+id/toggle"
android:layout_toEndOf="@+id/toggle"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sqrt"
android:layout_alignBottom="@+id/factorial"
android:layout_toRightOf="@+id/factorial"
android:layout_toEndOf="@+id/factorial"
android:text="@string/sqrt"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear"
android:id="@+id/clear"
android:layout_above="@+id/tan"
android:layout_toRightOf="@+id/log"
android:layout_toEndOf="@+id/log"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/backspace"
android:id="@+id/backSpace"
android:layout_above="@+id/sqrt"
android:layout_toRightOf="@+id/tan"
android:layout_toEndOf="@+id/tan"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pi"
android:id="@+id/pi"
android:layout_alignBottom="@+id/sqrt"
android:layout_alignLeft="@+id/backSpace"
android:layout_alignStart="@+id/backSpace"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/num7"
android:layout_below="@+id/toggle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/num8"
android:layout_alignBottom="@+id/num7"
android:layout_toRightOf="@+id/num7"
android:layout_toEndOf="@+id/num7"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/num9"
android:layout_alignBottom="@+id/num8"
android:layout_toRightOf="@+id/num8"
android:layout_toEndOf="@+id/num8"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/num4"
android:layout_below="@+id/num7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/num5"
android:layout_alignBottom="@+id/num4"
android:layout_alignLeft="@+id/num8"
android:layout_alignStart="@+id/num8"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/num6"
android:layout_alignBottom="@+id/num5"
android:layout_toRightOf="@+id/num5"
android:layout_toEndOf="@+id/num5"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/num1"
android:layout_below="@+id/num4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/num2"
android:layout_alignBottom="@+id/num1"
android:layout_alignLeft="@+id/num5"
android:layout_alignStart="@+id/num5"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/num3"
android:layout_alignBottom="@+id/num2"
android:layout_toRightOf="@+id/num2"
android:layout_toEndOf="@+id/num2"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/posneg"
android:id="@+id/posneg"
android:layout_below="@+id/num1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20dp"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/num0"
android:layout_alignBottom="@+id/posneg"
android:layout_toRightOf="@+id/posneg"
android:layout_toEndOf="@+id/posneg"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/dot"
android:layout_alignBottom="@+id/num0"
android:layout_toRightOf="@+id/num0"
android:layout_toEndOf="@+id/num0"
android:textSize="20dp"
android:nestedScrollingEnabled="false"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/divide"
android:id="@+id/divide"
android:layout_above="@+id/num6"
android:layout_toRightOf="@+id/num9"
android:layout_toEndOf="@+id/num9"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="@+id/multiply"
android:layout_above="@+id/num3"
android:layout_alignLeft="@+id/divide"
android:layout_alignStart="@+id/divide"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/minus"
android:layout_above="@+id/dot"
android:layout_alignLeft="@+id/multiply"
android:layout_alignStart="@+id/multiply"
android:textSize="20dp"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/plus"
android:layout_alignBottom="@+id/dot"
android:layout_alignLeft="@+id/minus"
android:layout_alignStart="@+id/minus"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/equal"
android:onClick="onClick"
android:layout_below="@+id/num0"
android:layout_toRightOf="@+id/num0"
android:layout_toEndOf="@+id/num0"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openBracket"
android:id="@+id/openBracket"
android:layout_alignTop="@+id/equal"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/closeBracket"
android:id="@+id/closeBracket"
android:layout_alignBottom="@+id/equal"
android:layout_toRightOf="@+id/openBracket"
android:layout_toEndOf="@+id/openBracket"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mode1"
android:id="@+id/mode"
android:layout_alignBottom="@+id/equal"
android:layout_toRightOf="@+id/equal"
android:layout_toEndOf="@+id/equal"
android:onClick="onClick"
android:textAllCaps="false"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/history2"
android:id="@+id/history"
android:layout_below="@+id/openBracket"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@drawable/mybutton"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1" />
</RelativeLayout>
activity_history.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.smartbingari.scientificcalculator.History"
tools:showIn="@layout/activity_history">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/deleteHistory"
android:id="@+id/deleteHistory"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="39dp"
android:onClick="onClick"
android:background="@drawable/mybutton" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_above="@+id/deleteHistory"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center" />
</RelativeLayout>
Inside yor drawable folder create a new resource xml file named: mybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="0dp"
/>
<solid
android:color="#4F4F4F"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#878787"
/>
</shape>
now in you MainActivity.java put this
package com.smartbingari.scientificcalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText e1,e2;
private int count=0;
private String expression="";
private String text="";
private Double result=0.0;
private DBHelper dbHelper;
private Button mode,toggle,square,xpowy,log,sin,cos,tan,sqrt,fact;
private int toggleMode=1;
private int angleMode=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
e2 = (EditText) findViewById(R.id.editText2);
mode = (Button) findViewById(R.id.mode);
toggle = (Button) findViewById(R.id.toggle);
square = (Button) findViewById(R.id.square);
xpowy = (Button) findViewById(R.id.xpowy);
log = (Button) findViewById(R.id.log);
sin = (Button) findViewById(R.id.sin);
cos = (Button) findViewById(R.id.cos);
tan = (Button) findViewById(R.id.tan);
sqrt= (Button) findViewById(R.id.sqrt);
fact = (Button) findViewById(R.id.factorial);
dbHelper=new DBHelper(this);
e2.setText("");
//tags to change the mode from degree to radian and vice versa
mode.setTag(1);
//tags to change the names of the buttons performing different operations
toggle.setTag(1);
}
public void onClick(View v)
{
toggleMode=(int)toggle.getTag();
angleMode=((int)mode.getTag());
switch (v.getId()) {
case R.id.toggle:
//change the button text if switch button is clicked
if(toggleMode==1)
{
toggle.setTag(2);
square.setText(R.string.cube);
xpowy.setText(R.string.tenpow);
log.setText(R.string.naturalLog);
sin.setText(R.string.sininv);
cos.setText(R.string.cosinv);
tan.setText(R.string.taninv);
sqrt.setText(R.string.cuberoot);
fact.setText(R.string.Mod);
}
else if(toggleMode==2)
{
toggle.setTag(3);
square.setText(R.string.square);
xpowy.setText(R.string.epown);
log.setText(R.string.log);
sin.setText(R.string.hyperbolicSine);
cos.setText(R.string.hyperbolicCosine);
tan.setText(R.string.hyperbolicTan);
sqrt.setText(R.string.inverse);
fact.setText(R.string.factorial);
}
else if(toggleMode==3)
{
toggle.setTag(1);
sin.setText(R.string.sin);
cos.setText(R.string.cos);
tan.setText(R.string.tan);
sqrt.setText(R.string.sqrt);
xpowy.setText(R.string.xpown);
}
break;
case R.id.mode:
//change the angle property for trignometric operations if mode button is clicked
if(angleMode==1)
{
mode.setTag(2);
mode.setText(R.string.mode2);
}
else
{
mode.setTag(1);
mode.setText(R.string.mode1);
}
break;
case R.id.num0:
e2.setText(e2.getText() + "0");
break;
case R.id.num1:
e2.setText(e2.getText() + "1");
break;
case R.id.num2:
e2.setText(e2.getText() + "2");
break;
case R.id.num3:
e2.setText(e2.getText() + "3");
break;
case R.id.num4:
e2.setText(e2.getText() + "4");
break;
case R.id.num5:
e2.setText(e2.getText() + "5");
break;
case R.id.num6:
e2.setText(e2.getText() + "6");
break;
case R.id.num7:
e2.setText(e2.getText() + "7");
break;
case R.id.num8:
e2.setText(e2.getText() + "8");
break;
case R.id.num9:
e2.setText(e2.getText() + "9");
break;
case R.id.pi:
e2.setText(e2.getText() + "pi");
break;
case R.id.dot:
if (count == 0 && e2.length() != 0) {
e2.setText(e2.getText() + ".");
count++;
}
break;
case R.id.clear:
e1.setText("");
e2.setText("");
count = 0;
expression = "";
break;
case R.id.backSpace:
text=e2.getText().toString();
if(text.length()>0)
{
if(text.endsWith("."))
{
count=0;
}
String newText=text.substring(0,text.length()-1);
//to delete the data contained in the brackets at once
if(text.endsWith(")"))
{
char []a=text.toCharArray();
int pos=a.length-2;
int counter=1;
//to find the opening bracket position
for(int i=a.length-2;i>=0;i--)
{
if(a[i]==')')
{
counter++;
}
else if(a[i]=='(')
{
counter--;
}
//if decimal is deleted b/w brackets then count should be zero
else if(a[i]=='.')
{
count=0;
}
//if opening bracket pair for the last bracket is found
if(counter==0)
{
pos=i;
break;
}
}
newText=text.substring(0,pos);
}
//if e2 edit text contains only - sign or sqrt or any other text functions
// at last then clear the edit text e2
if(newText.equals("-")||newText.endsWith("sqrt")||newText.endsWith("log")||newText.endsWith("ln")
||newText.endsWith("sin")||newText.endsWith("asin")||newText.endsWith("asind")||newText.endsWith("sinh")
||newText.endsWith("cos")||newText.endsWith("acos")||newText.endsWith("acosd")||newText.endsWith("cosh")
||newText.endsWith("tan")||newText.endsWith("atan")||newText.endsWith("atand")||newText.endsWith("tanh")
||newText.endsWith("cbrt"))
{
newText="";
}
//if pow sign is left at the last or divide sign
else if(newText.endsWith("^")||newText.endsWith("/"))
newText=newText.substring(0,newText.length()-1);
else if(newText.endsWith("pi")||newText.endsWith("e^"))
newText=newText.substring(0,newText.length()-2);
e2.setText(newText);
}
break;
case R.id.plus:
operationClicked("+");
break;
case R.id.minus:
operationClicked("-");
break;
case R.id.divide:
operationClicked("/");
break;
case R.id.multiply:
operationClicked("*");
break;
case R.id.sqrt:
if (e2.length() != 0) {
text = e2.getText().toString();
toggleMode=(int)toggle.getTag();
if(toggleMode==1)
e2.setText("sqrt(" + text + ")");
else if(toggleMode==2)
e2.setText("cbrt(" + text + ")");
else
e2.setText("1/(" + text + ")");
}
break;
case R.id.square:
if (e2.length() != 0) {
text = e2.getText().toString();
if(toggleMode==2)
e2.setText("(" + text + ")^3");
else
e2.setText("(" + text + ")^2");
}
break;
case R.id.xpowy:
if (e2.length() != 0) {
text = e2.getText().toString();
if(toggleMode==1)
e2.setText("(" + text + ")^");
else if(toggleMode==2)
e2.setText("10^(" + text + ")");
else
e2.setText("e^(" + text + ")");
}
break;
case R.id.log:
if (e2.length() != 0) {
text = e2.getText().toString();
if(toggleMode==2)
e2.setText("ln(" + text + ")");
else
e2.setText("log(" + text + ")");
}
break;
case R.id.factorial:
if (e2.length() != 0) {
text = e2.getText().toString();
if(toggleMode==2)
{
e1.setText("(" + text + ")%");
e2.setText("");
}
else
{
String res="";
try
{
CalculateFactorial cf=new CalculateFactorial();
int []arr=cf.factorial((int)Double.parseDouble(String.valueOf(new ExtendedDoubleEvaluator().evaluate(text))));
int res_size=cf.getRes();
if(res_size>20)
{
for (int i=res_size-1; i>=res_size-20; i--)
{
if(i==res_size-2)
res+=".";
res+=arr[i];
}
res+="E"+(res_size-1);
}
else
{
for (int i=res_size-1; i>=0; i--)
{
res+=arr[i];
}
}
e2.setText(res);
}
catch (Exception e)
{
if(e.toString().contains("ArrayIndexOutOfBoundsException"))
{
e2.setText("Result too big!");
}
else
e2.setText("Invalid!!");
e.printStackTrace();
}
}
}
break;
case R.id.sin:
if (e2.length() != 0) {
text = e2.getText().toString();
if(angleMode==1)
{
double angle=Math.toRadians(new ExtendedDoubleEvaluator().evaluate(text));
if(toggleMode==1)
e2.setText("sin(" + angle + ")");
else if(toggleMode==2)
e2.setText("asind(" + text + ")");
else
e2.setText("sinh(" + text + ")");
}
else
{
if(toggleMode==1)
e2.setText("sin(" + text + ")");
else if(toggleMode==2)
e2.setText("asin(" + text + ")");
else
e2.setText("sinh(" + text + ")");
}
}
break;
case R.id.cos:
if (e2.length() != 0) {
text = e2.getText().toString();
if(angleMode==1)
{
double angle=Math.toRadians(new ExtendedDoubleEvaluator().evaluate(text));
if(toggleMode==1)
e2.setText("cos(" + angle + ")");
else if(toggleMode==2)
e2.setText("acosd(" + text + ")");
else
e2.setText("cosh(" + text + ")");
}
else
{
if(toggleMode==1)
e2.setText("cos(" + text + ")");
else if(toggleMode==2)
e2.setText("acos(" + text + ")");
else
e2.setText("cosh(" + text + ")");
}
}
break;
case R.id.tan:
if (e2.length() != 0) {
text = e2.getText().toString();
if(angleMode==1)
{
double angle=Math.toRadians(new ExtendedDoubleEvaluator().evaluate(text));
if(toggleMode==1)
e2.setText("tan(" + angle + ")");
else if(toggleMode==2)
e2.setText("atand(" + text + ")");
else
e2.setText("tanh(" + text + ")");
}
else
{
if(toggleMode==1)
e2.setText("tan(" + text + ")");
else if(toggleMode==2)
e2.setText("atan(" + text + ")");
else
e2.setText("tanh(" + text + ")");
}
}
break;
case R.id.posneg:
if (e2.length() != 0) {
String s = e2.getText().toString();
char arr[] = s.toCharArray();
if (arr[0] == '-')
e2.setText(s.substring(1, s.length()));
else
e2.setText("-" + s);
}
break;
case R.id.equal:
/*for more knowledge on DoubleEvaluator and its tutorial go to the below link
http://javaluator.sourceforge.net/en/home/*/
if (e2.length() != 0) {
text = e2.getText().toString();
expression = e1.getText().toString() + text;
}
e1.setText("");
if (expression.length() == 0)
expression = "0.0";
try {
//evaluate the expression
result = new ExtendedDoubleEvaluator().evaluate(expression);
//insert expression and result in sqlite database if expression is valid and not 0.0
if (String.valueOf(result).equals("6.123233995736766E-17"))
{
result=0.0;
e2.setText(result + "");
}
else if(String.valueOf(result).equals("1.633123935319537E16"))
e2.setText("infinity");
else
e2.setText(result + "");
if (!expression.equals("0.0"))
dbHelper.insert("SCIENTIFIC", expression + " = " + result);
} catch (Exception e) {
e2.setText("Invalid Expression");
e1.setText("");
expression = "";
e.printStackTrace();
}
break;
case R.id.openBracket:
e1.setText(e1.getText() + "(");
break;
case R.id.closeBracket:
if(e2.length()!=0)
e1.setText(e1.getText() +e2.getText().toString()+ ")");
else
e1.setText(e1.getText() + ")");
break;
case R.id.history:
Intent i = new Intent(this, History.class);
i.putExtra("calcName", "SCIENTIFIC");
startActivity(i);
break;
}
}
private void operationClicked(String op) {
if (e2.length() != 0) {
String text = e2.getText().toString();
e1.setText(e1.getText() + text + op);
e2.setText("");
count = 0;
} else {
String text = e1.getText().toString();
if (text.length() > 0) {
String newText = text.substring(0, text.length() - 1) + op;
e1.setText(newText);
}
}
}
}
Create a new Java class and name it CalculateFactorial.java
and add this code
package com.smartbingari.scientificcalculator;
/**
* Created by Smart on 28-01-2017.
*/
public class CalculateFactorial
{
public static final int MAX=1000;
private int res_size;
private int res[]=new int[MAX];
CalculateFactorial()
{
res_size = 1;
}
public int getRes()
{
return res_size;
}
// This function finds factorial of large numbers and prints them
public int[] factorial(int n)
{
// Initialize result
res[0] = 1;
// Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
for (int x=2; x<=n; x++)
res_size = multiply(x, res_size);
return res;
}
// This function multiplies x with the number represented by res[].
// res_size is size of res[] or number of digits in the number represented
// by res[]. This function uses simple school mathematics for multiplication.
// This function may value of res_size and returns the new value of res_size
private int multiply(int x, int r)
{
int carry = 0; // Initialize carry
// One by one multiply n with individual digits of res[]
for (int i=0; i<r; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of 'prod' in res[]
carry = prod/10; // Put rest in carry
}
// Put carry in res and increase result size
while (carry!=0)
{
res[r] = carry%10;
carry = carry/10;
r++;
}
return r;
}
}
Create another Java Class and name is DBHelper.java
and add This
package com.smartbingari.scientificcalculator;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
/**
* Created by Smart on 13-03-2016.
*/
public class DBHelper extends SQLiteOpenHelper {
private static final String database_Name="HISTORY.DB";
private static final int database_Version=1;
private static final String TAG="DATABASE OPERATIONS";
private static final String table_Name="history";
private static final String column1="calculator_name";
private static final String column2="expression";
private static final String create_Table="CREATE TABLE "+table_Name+"("+column1+" TEXT,"+column2+" TEXT);";
SQLiteDatabase db;
public DBHelper(Context context) {
super(context,database_Name,null,database_Version);
Log.i(TAG,"Database Created / Opened");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(create_Table);
Log.i(TAG,"Table Created");
}
public void insert(String calcName,String expression)
{
db=getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(column1,calcName);
contentValues.put(column2, expression);
db.insert(table_Name, null, contentValues);
db.close();
}
public ArrayList<String> showHistory(String calcName)
{
db=getReadableDatabase();
Cursor cursor;
ArrayList<String> list=new ArrayList<String>();
String []selectionArgs={calcName};
//cursor=db.query(table_Name,columns,column1+" LIKE ?",selectionArgs,null,null,null);
cursor=db.rawQuery("select * from "+table_Name+" where "+column1+" = ?",selectionArgs);
if(cursor.moveToFirst())
{
do
{
String expression=cursor.getString(1);
list.add(expression);
}while (cursor.moveToNext());
}
db.close();
return list;
}
public void deleteRecords(String calcName)
{
db=getWritableDatabase();
String value[]={calcName};
int i=db.delete(table_Name, column1+"=?", value);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Create Another Java class and name it ExtendedDoubleEvaluator.java
Add this
package com.smartbingari.scientificcalculator;
/**
* Created by Smart on 13-03-2016.
*/
import com.fathzer.soft.javaluator.DoubleEvaluator;
import com.fathzer.soft.javaluator.Function;
import com.fathzer.soft.javaluator.Parameters;
import java.util.Iterator;
/** A subclass of DoubleEvaluator that supports SQRT function.
*/
public class ExtendedDoubleEvaluator extends DoubleEvaluator {
/** Defines the new function (square root).*/
private static final Function SQRT = new Function("sqrt", 1);
private static final Function CBRT = new Function("cbrt", 1);
private static final Function ASIND = new Function("asind", 1);
private static final Function ACOSD = new Function("acosd", 1);
private static final Function ATAND = new Function("atand", 1);
private static final Parameters PARAMS;
static {
// Gets the default DoubleEvaluator's parameters
PARAMS = DoubleEvaluator.getDefaultParameters();
// add the new sqrt function to these parameters
PARAMS.add(SQRT);
PARAMS.add(CBRT);
PARAMS.add(ASIND);
PARAMS.add(ACOSD);
PARAMS.add(ATAND);
}
public ExtendedDoubleEvaluator() {
super(PARAMS);
}
@Override
public Double evaluate(Function function, Iterator<Double> arguments, Object evaluationContext) {
if (function == SQRT) {
// Implements the new function
return Math.sqrt(arguments.next());
}
else if(function == CBRT)
{
return Math.cbrt(arguments.next());
}
else if(function == ASIND)
{
return Math.toDegrees(Math.asin(arguments.next()));
}
else if(function == ACOSD)
{
return Math.toDegrees(Math.acos(arguments.next()));
}
else if(function == ATAND)
{
return Math.toDegrees(Math.atan(arguments.next()));
}
else {
// If it's another function, pass it to DoubleEvaluator
return super.evaluate(function, arguments, evaluationContext);
}
}
}
Create a file again and name it History.java
and Add this
package com.smartbingari.scientificcalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class History extends AppCompatActivity {
private ListView lv;
private DBHelper dbHelper;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
private String calcName="";
private String []EmptyList={"There is no history yet"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
lv=(ListView)findViewById(R.id.listView);
dbHelper=new DBHelper(this);
calcName=getIntent().getStringExtra("calcName");
list=dbHelper.showHistory(calcName);
if(!list.isEmpty())
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
else
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,EmptyList);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
dbHelper.deleteRecords(calcName);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,EmptyList);
lv.setAdapter(adapter);
}
}
In you res/valuesstrings.xml file put this:
<resources>
<string name="app_name">Scientific Calculator</string>
<string name="clear">C</string>
<string name="backspace">⌫</string>
<string name="divide">÷</string>
<string name="sqrt">√</string>
<string name="cuberoot">\u221B</string>
<string name="square">x²</string>
<string name="cube">x\u00B3</string>
<string name="tenpow">10\u207F</string>
<string name="sininv">sin\u207B\u00B9</string>
<string name="cosinv">cos\u207B\u00B9</string>
<string name="taninv">tan\u207B\u00B9</string>
<string name="posneg">±</string>
<string name="arrow">↑</string>
<string name="xpown">xⁿ</string>
<string name="epown">eⁿ</string>
<string name="pi">π</string>
<string name="title_activity_unit_area">UnitArea</string>
<string name="title_activity_unit_length">UnitLength</string>
<string name="title_activity_unit_weight">UnitWeight</string>
<string name="title_activity_unit_temperature">UnitTemperature</string>
<string name="openBracket">(</string>
<string name="closeBracket">)</string>
<string name="history2">History</string>
<string-array name="area">
<item>Sq. Millimeter</item>
<item>Sq. Centimeter</item>
<item>Sq Meter</item>
<item>Sq. Kilometer</item>
<item>Acre</item>
<item>Hectare</item>
</string-array>
<string-array name="length">
<item>Nanometer</item>
<item>Millimeter</item>
<item>Centimeter</item>
<item>Meter</item>
<item>Kilometer</item>
<item>Inches</item>
<item>Foot</item>
<item>Yards</item>
<item>Mile</item>
</string-array>
<string-array name="temperature">
<item>Celsius</item>
<item>Farenheit</item>
<item>Kelvin</item>
</string-array>
<string-array name="weight">
<item>Milligrams</item>
<item>Centigrams</item>
<item>Decigrams</item>
<item>Grams</item>
<item>Kilograms</item>
<item>Metric tonnes</item>
<item>Pounds</item>
<item>Ounces</item>
</string-array>
<string name="title_activity_history">History</string>
<string name="deleteHistory">Delete History</string>
<string name="mode1">Degree</string>
<string name="mode2">Radian</string>
<string name="toggle">SWITCH</string>
<string name="sin">sin</string>
<string name="cos">cos</string>
<string name="tan">tan</string>
<string name="naturalLog">ln</string>
<string name="hyperbolicSine">sinh</string>
<string name="hyperbolicCosine">cosh</string>
<string name="hyperbolicTan">tanh</string>
<string name="log">log</string>
<string name="Mod">%</string>
<string name="inverse">1/x</string>
<string name="factorial">x!</string>
</resources>
Your AndroidManifest.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smartbingari.scientificcalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".History" />
</application>
</manifest>
If you have any question please use the comment box.
Download Source Code Here
How to Create a Scientific Calculator for Android
Reviewed by Mr. SAM
on
December 31, 2017
Rating:
No comments: