Sie sind auf Seite 1von 25

SCIENTIFIC CALCULATOR

1. ScientificCalculator.java
package com.midmax.calculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ScientificCalculator extends AppCompatActivity {
//object creates
TextView txtbtn;
float mValueOne , mValueTwo ;
double a;
double ans=0;
boolean mAddition , mSubtract ,mMultiplication ,mDivision,mReminder ,
mNoPower,istpower2ndno,mSin,mArithmetic,mCos,mTan,mSinH,mCosH,mTanH;
boolean piecheck=false;
Button clearbutton,
dividebutton,
multiplybutton,
deletebutton,
button7,
button8,
button9,
minusbutton,
button4,
button5,
button6,
plusbutton,
button1,
button2,
button3,
percentagebutton,
button0,
pointbutton,
equalbutton,
xcubebutton,
squarebutton,
xfactorialbutton,
logbutton,
exponentbutton,
lnbutton,
sinbutton,
cosbutton,
tanbutton,
oneoverxbutton,
sinhbutton,
coshbutton,
tanhbutton,
varpower,
cuberoot,
tenexponent,
piebutton,
squarerootbutton,
nasbutton,
cuberootbutton,
modulosbutton,
varNoPower,
epowerbtn,
stpower2nd,
ansbuttton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scientific_calculator);
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);

scientific_operation();
}
public void scientific_operation(){
txtbtn=(TextView) findViewById(R.id.txt);
clearbutton=(Button) findViewById(R.id.clearrbtn);
dividebutton=(Button) findViewById(R.id.dividebtn);
multiplybutton=(Button) findViewById(R.id.multiplybtn);
deletebutton=(Button) findViewById(R.id.deletebtn);
button7=(Button) findViewById(R.id.sevenbtn);
button8=(Button) findViewById(R.id.eightbtn);
button9=(Button) findViewById(R.id.ninebtn);
minusbutton=(Button) findViewById(R.id.minusbtn);
button4=(Button) findViewById(R.id.fourbtn);
button5=(Button) findViewById(R.id.fivebtn);
button6=(Button) findViewById(R.id.sixbtn);
plusbutton=(Button) findViewById(R.id.plusbtn);
button1=(Button) findViewById(R.id.onebtn);
button2=(Button) findViewById(R.id.twobtn);
button3=(Button) findViewById(R.id.threebtn);
percentagebutton=(Button) findViewById(R.id.modulousbtn);
button0=(Button) findViewById(R.id.zerobtn);
pointbutton=(Button) findViewById(R.id.pointbtn);
equalbutton=(Button) findViewById(R.id.equalbtn);
squarebutton=(Button) findViewById(R.id.squarebtn);
xcubebutton=(Button) findViewById(R.id.xcubebtn);
oneoverxbutton=(Button) findViewById(R.id.onedividebtn);
exponentbutton=(Button) findViewById(R.id.expbtn);
logbutton=(Button) findViewById(R.id.logbtn);
squarerootbutton=(Button) findViewById(R.id.squarerootbtn);
xfactorialbutton=(Button) findViewById(R.id.xfactorialbtn);
sinbutton=(Button) findViewById(R.id.sinbtn);
cosbutton=(Button) findViewById(R.id.cosbtn);
tanbutton=(Button)findViewById(R.id.tanbtn);
sinhbutton=(Button)findViewById(R.id.sinhbtn);
coshbutton=(Button)findViewById(R.id.coshbtn);
tanhbutton=(Button)findViewById(R.id.tanhbtn);
lnbutton=(Button) findViewById(R.id.lnbtn) ;
ansbuttton=(Button) findViewById(R.id.ansbtn);
piebutton=(Button) findViewById(R.id.piebtn);
deletebutton=(Button) findViewById(R.id.deletebtn);
tenexponent=(Button) findViewById(R.id.tenexponent) ;
cuberootbutton=(Button)findViewById(R.id.cuberootbtn);
modulosbutton=(Button)findViewById(R.id.modulousbtn);
epowerbtn=(Button) findViewById(R.id.epowerbtn);
stpower2nd=(Button) findViewById(R.id.istpower2nd);
stpower2nd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
istpower2ndno = true;
txtbtn.setText(null);
}
catch (Exception e) {
}
}
});
epowerbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Double n= Double.parseDouble(txtbtn.getText().toString());
Double exp = (Double) Math.pow(2.718281828, n);
txtbtn.setText(exp+"");
}
catch (Exception e){
}
}
});
varNoPower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try{
// mValueOne = Float.parseFloat(txtbtn.getText() + "");
// mNoPower = true;
// txtbtn.setText(null);
// }
// catch (Exception e) {
// // Handle the error/exception
// }

}
});
modulosbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mReminder = true;
txtbtn.setText(null);
}
catch (Exception e) {
}
}
});
cuberootbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Math.cbrt(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
}
}
});
tenexponent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
int n= Integer.parseInt(txtbtn.getText().toString());
int exp = (int) Math.pow(10, n);
txtbtn.setText(exp+"");
}
catch (Exception e){
}
}
});
deletebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String str;
str = txtbtn.getText().toString();
str = str.substring(0, str.length() - 1);
txtbtn.setText(str);
}
catch (Exception e){
}
}
});
piebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if(txtbtn.getText().toString()!=null) {
// a=Double.parseDouble(txtbtn.getText().toString()) +Math.PI;
// txtbtn.setText("" +a );
// }
// else {
txtbtn.setText(Math.PI + "");
// }
}
});
lnbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Double.parseDouble(txtbtn.getText().toString());
double result = (-Math.log(1-a))/a;
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + result);
}
catch(Exception e){
}
}
});
tanhbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.tanh(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
//
// }
txtbtn.setText("tanh");
mTanH=true;
}
});
coshbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.cosh(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
//
// }
txtbtn.setText("cosh");
mCosH=true;
}
});
sinhbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.sinh(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
// }
txtbtn.setText("sinh");
mSinH=true;

}
});
sinbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.sin(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
// }
txtbtn.setText("sin");
mSin=true;
}
});
cosbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.cos(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
// }
txtbtn.setText("cos");
mCos=true;
}
});
tanbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// try {
// a = Math.tan(Double.parseDouble(txtbtn.getText().toString()));
// txtbtn.setText("");
// txtbtn.setText(txtbtn.getText().toString() + a);
// }
// catch(Exception e){
// }
txtbtn.setText("tan");
mTan=true;
}
});
xfactorialbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Double.parseDouble(txtbtn.getText().toString());
int er = 0; double i, s = 1;
if (a< 0) {
er = 20;
}
else {
for (i = 2; i <= a; i += 1.0)
s *= i;
}
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + s);
}
catch(Exception e){
}
}
});
squarerootbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Math.sqrt(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
}
}
});
logbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Math.log(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
}
}
});
exponentbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Math.exp(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
}
}
});
oneoverxbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = 1 / Double.parseDouble(txtbtn.getText().toString());
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
}
}
});
squarebutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
try{
a = Math.pow(Double.parseDouble(txtbtn.getText().toString()), 2);
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a+"");
}
catch (Exception e){
}
}
});
xcubebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
a = Math.pow(Double.parseDouble(txtbtn.getText().toString()), 3);
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch (Exception e){
}
}
});
clearbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("");
}
});
dividebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText(null);
}
});
//////////////
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"1");
mArithmetic=true;
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"2");
mArithmetic=true;
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"3");
mArithmetic=true;
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"4");
mArithmetic=true;
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"5");
mArithmetic=true;
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"6");
mArithmetic=true;
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"7");
mArithmetic=true;
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"8");
mArithmetic=true;
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"9");
mArithmetic=true;
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+"0");
mArithmetic=true;
}
});
plusbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mAddition = true;
txtbtn.setText(null);
}
catch (Exception e) {
// Handle the error/exception
}
}
});
minusbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mSubtract = true;
txtbtn.setText(null);
}
catch (Exception e) {
// Handle the error/exception
}
}
});
multiplybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mMultiplication = true;
txtbtn.setText(null);
}
catch (Exception e) {
// Handle the error/exception
}
}
});
dividebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mDivision = true;
txtbtn.setText(null);
}
catch (Exception e) {
// Handle the error/exception
}
}
});
equalbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mSin==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(3);
a=Math.sin(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mSin=false;
}
}
if(mCos==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(3);
a=Math.cos(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mCos=false;
}
}
if(mTan==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(3);
a=Math.sin(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mTan=false;
}
}
if(mSinH==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(4);
a=Math.sinh(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mSinH=false;
}
}
if(mCosH==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(4);
a=Math.cosh(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mCosH=false;
}
}
if(mTanH==true){
if(mArithmetic==true){
String str;
str=txtbtn.getText().toString();
str=str.substring(4);
a=Math.cosh(Double.parseDouble(str));
txtbtn.setText(a+"");
mArithmetic=false;
mTanH=false;
}
}
if (istpower2ndno == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
int exp = (int) Math.pow(mValueOne, mValueTwo);
txtbtn.setText(exp+"");
istpower2ndno=false;
}
if (mAddition == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne + mValueTwo +"");
mAddition=false;
}
if (mReminder == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne % mValueTwo +"");
mReminder=false;
}
if(mNoPower==true) {
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
int exp = (int) Math.pow(mValueOne, mValueTwo);
txtbtn.setText(exp+"");
mNoPower=false;
}
if (mSubtract == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne - mValueTwo+"");
mSubtract=false;
}
if (mMultiplication == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne * mValueTwo+"");
mMultiplication=false;
}
if (mDivision == true){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne / mValueTwo+"");
mDivision=false;
}
ans=Double.parseDouble(txtbtn.getText().toString());
}
});
clearbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(null);
// a = Math.sin(Double.parseDouble(txtbtn.getText()+""));
// txtbtn.setText(""+a);
}
});
pointbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText()+".");
}
});
ansbuttton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText(ans+"");
}
});
////////////////////
}
}
2. Activity Scientific Calculator
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/backgroundall"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
tools:context=".ScientificCalculator">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="215dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:gravity="bottom"
android:hint="0"
android:padding="20dp"
android:textColor="#F7F7F7"
android:textSize="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/xfactorialbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="x!"
android:textColor="#F7F7F7"
android:textStyle="italic" />
<Button
android:id="@+id/logbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:autoText="false"
android:background="@drawable/buttoncolor"
android:text="log"
android:textColor="#F7F7F7"
android:textStyle="normal" />
<Button
android:id="@+id/epowerbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/exponent"
android:textColor="#F7F7F7"
android:textStyle="italic" />
<Button
android:id="@+id/lnbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="ln"
android:textColor="#F7F7F7"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/squarerootbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/square_root_symbol"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/istpower2nd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="^"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/sinbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="sin"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/cosbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="cos"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/tanbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="tan"
android:textColor="#F7F7F7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/onedividebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="1/x"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/sinhbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="sinh"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/coshbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="cosh"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/tanhbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="tanh"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/squarebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/xsquare"
android:textColor="#F7F7F7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/cuberootbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/cube_root_symbol"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/tenexponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="x10x"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/expbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="Exp"
android:textColor="#F7F7F7" />
<Button
android:id="@+id/modulousbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="%"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/xcubebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/xcube"
android:textColor="#F7F7F7"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/sevenbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="7"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/eightbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="8"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/ninebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="9"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/deletebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/delacbtn"
android:text="DEL"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/clearrbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/delacbtn"
android:text="AC"
android:textColor="#F7F7F7"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/fourbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="4"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/fivebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="5"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/sixbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="6"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/plusbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="+"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/minusbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="-"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/onebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="1"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/twobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="2"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/threebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="3"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/multiplybtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="x"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/dividebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="/"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/zerobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="0"
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/pointbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="."
android:textColor="#F7F7F7"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/piebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="@string/pie_symbol"
android:textColor="#F7F7F7"
android:textStyle="bold" />

<Button
android:id="@+id/ansbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/buttoncolor"
android:text="ANS"
android:textColor="#F7F7F7"
android:textStyle="bold" />
<Button
android:id="@+id/equalbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/equalbtn"
android:text="="
android:textColor="#F7F7F7"
android:textSize="23dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
KALKULATOR STANDARD

1. MainActivity.java
import android.widget.Button;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
public class MainActivity extends AppCompatActivity {
// IDs of all the numeric buttons
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo,
R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight,
R.id.btnNine};
// IDs of all the operator buttons
private int[] operatorButtons = {R.id.btnAdd, R.id.btnSubtract,
R.id.btnMultiply, R.id.btnDivide};
// TextView used to display the output
private TextView txtScreen;
// Represent whether the lastly pressed key is numeric or not
private boolean lastNumeric;
// Represent that current state is in error or not
private boolean stateError;
// If true, do not allow to add another DOT
private boolean lastDot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txtScreen = (TextView) findViewById(R.id.txtScreen);
setNumericOnClickListener();
setOperatorOnClickListener();
}
private void setNumericOnClickListener() {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
if (stateError) {
txtScreen.setText(button.getText());
stateError = false;
} else {
txtScreen.append(button.getText());
}
lastNumeric = true;
}
};
for (int id : numericButtons) {
findViewById(id).setOnClickListener(listener);
}
}
private void setOperatorOnClickListener() {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (lastNumeric && !stateError) {
Button button = (Button) v;
txtScreen.append(button.getText());
lastNumeric = false;
lastDot = false;
}
}
};
for (int id : operatorButtons) {
findViewById(id).setOnClickListener(listener);
}
findViewById(R.id.btnDot).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (lastNumeric && !stateError && !lastDot) {
txtScreen.append(".");
lastNumeric = false;
lastDot = true;
}
}
});
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
txtScreen.setText("");
lastNumeric = false;
stateError = false;
lastDot = false;
}
});
findViewById(R.id.btnEqual).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
onEqual();
}
});
}
private void onEqual() {
if (lastNumeric && !stateError) {
String txt = txtScreen.getText().toString();
Expression expression = new ExpressionBuilder(txt).build();
try {
double result = expression.evaluate();
txtScreen.setText(Double.toString(result));
lastDot = true;
} catch (ArithmeticException ex) {
txtScreen.setText("Error");
stateError = true;
lastNumeric = false;
}
}
}
}

2. activity_main
<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=".MainActivity">
<TextView
android:id="@+id/txtScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="right|center_vertical"
android:maxLength="16"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:typeface="serif"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtScreen"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnSeven"
android:text="7"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnEight"
android:text="8" />
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnNine"
android:text="9"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnDivide"
android:text="/"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnFour"
android:text="4"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnFive"
android:text="5"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnSix"
android:text="6"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/btnMultiply"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnOne"
android:text="1"/>

<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnTwo"
android:text="2"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnThree"
android:text="3"/>
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnSubtract"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnDot"
android:text="." />
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnZero"
android:text="0" />
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnClear"
android:text="C" />
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnAdd"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:id="@+id/btnEqual"
android:text="=" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Das könnte Ihnen auch gefallen